Modify system brightness in Android player
/** * Player Activity */public class Playeractivity extends Activity {/** * system automatically adjusts brightness */private Boolean Isautobrig Htness = 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); /** * Open the Player interface to determine if the system is set to auto brightness * Set the system brightness if the system brightness set automatic brightness, you need to turn off the auto brightness */isautobrightness = deviceutil.isautobrightness (This) ; if (isautobrightness) {deviceutil.stopautobrightness (this);}} @Overrideprotected void Onresume () {/* * Player request screen is required to be written in the Onresume method, if written in the OnCreate () method, * When the user presses home after playback, the screen always fails */ Deviceutil.requirescreenon (this);/** * Resolve User Press home back to player brightness mode for auto brightness re-method: (Auto brightness on playeractivity--> Press home--( Enter system settings at this time * system brightness is non-auto brightness) modified to auto brightness--back to the player) */isautobrightness = deviceutil.isautobrightness (this); isautobrightness) {deviceutil.stopautobrightness (this);}} @Overridepublic Boolean OnkEydown (int keycode, keyevent event) {//TODO auto-generated method Stubint currsettingbrighiness = Deviceutil.getsystemsc Reenbrightness (this), switch (keycode) {case keyevent.keycode_dpad_up://system brightness plus addsystembrighiness ( currsettingbrighiness) break;case keyevent.keycode_dpad_down://System Brightness reduction minussystembrighiness (currSettingBrighiness ); break;default:break;} Return Super.onkeydown (KeyCode, event);} /** * System Brightness plus * * @param currsettingbrighiness * System current brightness */private void addsystembrighiness (int Currsettingbrighi Ness) {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);}} /** * System Brightness minus * * @param currsettingbrighiness * System current brightness */public void minussystembrighiness (int currsettingbrigh iness) {if (currsettingbrighiness >= system_brightness_change_values) {setsystembrighiness = Currsettingbrighiness-system_brightness_change_values;d Eviceutil.setsystemscreenbrightness (this, setsystembrighiness);}}}
/** * Deviceutil Tool class */public class Deviceutil {/** * get system brightness * Take value between (0--255) */public static int GE Tsystemscreenbrightness (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; }/** * Set system brightness * @param systembrightness The luminance value returned is an integer value between 0-255 */public static Boolean SETSYSTEMSCR Eenbrightness (context context, int systembrightness) {return Settings.System.putInt (Context.getcontentresolver (), S Ettings. system.screen_brightness,systembrightness); }/** * system automatically adjusts brightness * return true is auto adjust brightness return false not automatically adjusts brightness */public static Boolean isautobrightness (Activity activit Y) {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;}} /** * Turn off system auto Adjust brightness */public static void stopautobrightness (activity activity) {Settings.System.putInt ( Activity.getcontentresolver (), Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_ MANUAL);} /** * Turn on system auto Adjust brightness */public static void startautobrightness (activity activity) {Settings.System.putInt ( Activity.getcontentresolver (), Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_ AUTOMATIC);} /** * Request Screen Solid * @param activity */public static void Requirescreenon (activity activity) {Activity.getwindow (). SetFlags ( WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);} /** * Cancel Solid screen * @param activity */public static void Releasescreenon (activity activity) {Activity.getWindow (). Clearflags (WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);}}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android Setting System brightness