Android动态禁用或开启屏幕旋转工具

package com.gwtsz.gts2.util;

import android.content.Context;
import android.provider.Settings;
import android.provider.Settings.SettingNotFoundException;

/**
 * 重力感应器开关
 * 环绕手机屏幕旋转的设置功能编写的方法
 * @author Wilson
 */
public class SensorUtil {
	/**
	 * 打开重力感应。即设置屏幕可旋转
	 * @param context
	 */
	public static void openSensor(Context context){
		Settings.System.putInt(context.getContentResolver(),Settings.System.ACCELEROMETER_ROTATION, 1);
	}
	
	/**
	 * 关闭重力感应,即设置屏幕不可旋转
	 * @param context
	 */
	public static void closeSensor(Context context){
		Settings.System.putInt(context.getContentResolver(),Settings.System.ACCELEROMETER_ROTATION, 0);
	}
	
	/**
	 * 获取屏幕旋转功能开启状态
	 * @param context
	 * @return
	 */
	public static int getSensorState(Context context){
		int sensorState = 0;
		try {
			sensorState = Settings.System.getInt(context.getContentResolver(), Settings.System.ACCELEROMETER_ROTATION);
			return sensorState;
		} catch (SettingNotFoundException e) {
			e.printStackTrace();
		}
		return sensorState;
	}
	
	/**
	 * 推断屏幕旋转功能是否开启
	 */
	public static boolean isOpenSensor(Context context){
		boolean isOpen = false;
		if(getSensorState(context) == 1){
			isOpen = true;
		}else if(getSensorState(context) == 0){
			isOpen = false;
		}
		return isOpen;
	}
}

posted on 2017-08-10 12:38  blfbuaa  阅读(728)  评论(0编辑  收藏  举报