Android 設定系統亮度
阿新 • • 發佈:2019-02-06
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); } }