Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android 設置系統亮度

Android 設置系統亮度

編輯:關於Android編程

Android 播放器中修改系統亮度

 

/**
 * 播放器Activity
 */
public class PlayerActivity extends Activity {
   
    /**
	 * 系統是否自動調節亮度
	 */
    private boolean isAutoBrightness = false;

    /**
	 * 系統亮度變化值
	 */
	private static final int SYSTEM_BRIGHTNESS_CHANGE_VALUES = 25;

	/**
	 * 最大系統音量值
	 */
	private static final int MAX_SYSTEM_BRIGHTNESS_VALUES = 255;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {		
		super.onCreate(savedInstanceState); 
		/**
         * 打開播放器界面判斷系統是否設置了自動亮度
		 * 設置系統亮度時如果系統亮度設置了自動亮度,需要先關閉自動亮度
		 */
		isAutoBrightness = DeviceUtil.isAutoBrightness(this);
		if (isAutoBrightness) {
			DeviceUtil.stopAutoBrightness(this);
		}
	}
	
	@Override
	protected void onResume() {
	    /*
		 * 播放器請求屏幕常亮需要寫在onResume方法中,如果寫在onCreate()方法中,
		 * 用戶在播放時按HOME後,再次回到播放器,屏幕常亮失敗
		 */
	    DeviceUtil.requireScreenOn(this);
		
		/**
		 * 解決用戶按HOME後回到播放器亮度模式為自動亮度  重新方法:(自動亮度打開PlayerActivity--> 按HOME --> (進入系統設置此時
		 * 系統亮度為非自動亮度)修改為自動亮度 --> 回到播放器)
		 */
		isAutoBrightness = DeviceUtil.isAutoBrightness(this);
		if (isAutoBrightness) {
			DeviceUtil.stopAutoBrightness(this);
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		int currSettingBrighiness = DeviceUtil.getSystemScreenBrightness(this);
		switch (keyCode) {
		case KeyEvent.KEYCODE_DPAD_UP:
			// 系統亮度加
		    addSystemBrighiness(currSettingBrighiness);
			break;
		case KeyEvent.KEYCODE_DPAD_DOWN:
			// 系統亮度減
			minusSystemBrighiness(currSettingBrighiness);
			break;
		default:
			break;
		}
		return super.onKeyDown(keyCode, event);
	}
	
	/**
	 * 系統亮度加
	 * 
	 * @param currSettingBrighiness
	 *            系統當前亮度
	 */
	private void addSystemBrighiness(int currSettingBrighiness) {
		if (currSettingBrighiness < MAX_SYSTEM_BRIGHTNESS_VALUES
				&& (currSettingBrighiness + SYSTEM_BRIGHTNESS_CHANGE_VALUES) < MAX_SYSTEM_BRIGHTNESS_VALUES) {
			setSystemBrighiness = currSettingBrighiness
					+ SYSTEM_BRIGHTNESS_CHANGE_VALUES;
			DeviceUtil.setSystemScreenBrightness(this, setSystemBrighiness);
		}
	}
	
	/**
	 * 系統亮度減
	 * 
	 * @param currSettingBrighiness
	 *            系統當前亮度
	 */
	public void minusSystemBrighiness(int currSettingBrighiness) {
		if (currSettingBrighiness >= SYSTEM_BRIGHTNESS_CHANGE_VALUES) {
			setSystemBrighiness = currSettingBrighiness
					- SYSTEM_BRIGHTNESS_CHANGE_VALUES;
			DeviceUtil.setSystemScreenBrightness(this, setSystemBrighiness);
		}
	}
}

/**
 * DeviceUtil 工具類
 */
public class DeviceUtil {
    /**
     * 獲取系統亮度 
     * 取值在(0 -- 255)之間
     */
    public static int getSystemScreenBrightness(Context context) {
        int values = 0;
        try {
            values = Settings.System.getInt(context.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS);
        } catch (SettingNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
        return values;
    }
    
    /**
     * 設置系統亮度
     * @param systemBrightness 返回的亮度值是處於0-255之間的整型數值
     */
    public static boolean setSystemScreenBrightness(Context context, int systemBrightness) {
       return Settings.System.putInt(context.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS,systemBrightness);
    }
	
	/**
	 * 系統是否自動調節亮度
	 * return true 是自動調節亮度   return false 不是自動調節亮度 
	 */
	public static boolean isAutoBrightness(Activity activity){
		int autoBrightness = Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC;
		try {
			autoBrightness = Settings.System.getInt(activity.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE);
		} catch (SettingNotFoundException e) {
			e.printStackTrace();
		}
		if (autoBrightness == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC) {
			return true;
		} else {
			return false;
		}
	}
	
	/**
	 * 關閉系統自動調節亮度
	 */
	public static void stopAutoBrightness(Activity activity){
		Settings.System.putInt(activity.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, 
				Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
	}
	
	/**
	 * 打開系統自動調節亮度
	 */
	public static void startAutoBrightness(Activity activity) {
		Settings.System.putInt(activity.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, 
				Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
	}
	
	/**
	 * 請求屏幕常亮
	 * @param activity
	 */
	public static void requireScreenOn(Activity activity){
		activity.getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, 
				WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
	}
	
	/**
	 * 取消屏幕常亮
	 * @param activity
	 */
	public static void releaseScreenOn(Activity activity){
		activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
	}
}

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved