zoukankan      html  css  js  c++  java
  • 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;
    	}
    }
    

  • 相关阅读:
    1600802101
    Android第二次作业
    android 第一次作业
    团队作业—项目答辩
    软件工程—团队作业2.2
    软件工程—团队作业2
    软件工程—团队作业1
    第一篇博客
    Android第四次作业
    作业3
  • 原文地址:https://www.cnblogs.com/blfbuaa/p/7338660.html
Copyright © 2011-2022 走看看