Set system brightness for Android
Modify system brightness in Android player
/*** Player Activity */public class PlayerActivity extends Activity {/*** whether the system automatically adjusts the brightness */private boolean isAutoBrightness = false; /*** system brightness change value */private static final int SYSTEM_BRIGHTNESS_CHANGE_VALUES = 25;/*** Maximum system volume value */private static final int MAX_SYSTEM_BRIGHTNESS_VALUES = 255; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState);/*** enable the player interface to determine whether the system has set the automatic brightness * If the system brightness is set to the automatic brightness, disable automatic brightness */isAutoBrightness = DeviceUtil. isAutoBrightness (this); if (isAutoBrightness) {DeviceUtil. stopAutoBrightness (this) ;}@ Overrideprotected void onResume () {/** the player request screen must always be highlighted in the onResume method. If it is written in the onCreate () method, * After the user presses HOME during playback, the player returns to the player again. The screen is always on and fails */DeviceUtil. requireScreenOn (this ); /*** the solution is to automatically return to the player brightness mode after you press HOME: (enable PlayerActivity with automatic brightness --> press HOME --> (go to system settings, then * The system brightness is not automatically brightness .)) change to automatic brightness --> return to the player) */isAutoBrightness = DeviceUtil. isAutoBrightness (this); if (isAutoBrightness) {DeviceUtil. stopAutoBrightness (this) ;}@ Overridepublic boolean onKeyDown (int keyCode, KeyEvent event) {// TODO Auto-generated method stubint currSettingBrighiness = DeviceUtil. getSystemScreenBrightness (this); switch (keyCode) {case KeyEvent. KEYCODE_DPAD_UP: // Add addSystemBrighiness (currSettingBrighiness); break; case KeyEvent. KEYCODE_DPAD_DOWN: // system brightness minus minusSystemBrighiness (currSettingBrighiness); break; default: break;} return super. onKeyDown (keyCode, event);}/*** system brightness plus ** @ param currSettingBrighiness * Current System brightness */private void addSystemBrighiness (int currSettingBrighiness) {if (currSettingBrighiness <percent & (currSettingBrighiness + SYSTEM_BRIGHTNESS_CHANGE_VALUES) <percent) {setSystemBrighiness = currSettingBrighiness + percent; DeviceUtil. setSystemScreenBrightness (this, setSystemBrighiness);}/*** system brightness reduction ** @ param currSettingBrighiness * Current System brightness */public void minusSystemBrighiness (int currSettingBrighiness) {if (currSettingBrighiness> = SYSTEM_BRIGHTNESS_CHANGE_VALUES) {setSystemBrighiness = currSettingBrighiness-SYSTEM_BRIGHTNESS_CHANGE_VALUES; DeviceUtil. setSystemScreenBrightness (this, setSystemBrighiness );}}}
/*** DeviceUtil tool class */public class DeviceUtil {/*** obtain system brightness * between (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;}/*** sets the system brightness * @ param systemBrightness returns an integer value between 0 and */public static boolean setSystemScreenBrightness (Context context, int systemBrightness) {return Settings. system. putInt (context. getContentResolver (), Settings. system. SCREEN_BRIGHTNESS, systemBrightness);}/*** whether the system automatically adjusts the brightness * return true indicates that the brightness is automatically adjusted. return false indicates that the brightness is not automatically adjusted. */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;}/*** disable automatic brightness adjustment by the system */public static void stopAutoBrightness (Activity activity) {Settings. system. putInt (activity. getContentResolver (), Settings. system. SCREEN_BRIGHTNESS_MODE, Settings. system. SCREEN_BRIGHTNESS_MODE_MANUAL);}/*** turn on the system to automatically adjust the brightness */public static void startAutoBrightness (Activity activity) {Settings. system. putInt (activity. getContentResolver (), Settings. system. SCREEN_BRIGHTNESS_MODE, Settings. system. SCREEN_BRIGHTNESS_MODE_AUTOMATIC);}/*** the request screen is always on * @ param activity */public static void requireScreenOn (Activity activity) {activity. getWindow (). setFlags (WindowManager. layoutParams. FLAG_KEEP_SCREEN_ON, WindowManager. layoutParams. FLAG_KEEP_SCREEN_ON);}/*** cancel the screen always on * @ param activity */public static void releaseScreenOn (Activity activity) {activity. getWindow (). clearFlags (WindowManager. layoutParams. FLAG_KEEP_SCREEN_ON );}}