Android brightness adjustment tool: BrightnessUtils; android brightness adjustment
If you need to reprint please indicate the source: http://blog.csdn.net/itas109
QQ technology exchange group: 129518033
The project requires a tool class to adjust the screen
/** Android brightness adjustment tool class ** by itas109 * http://blog.csdn.net/itas109 ** Note: you need to add setting permissions * <uses-permission android: name = "android. permission. WRITE_SETTINGS "/> */public class BrightnessUtils {// determines whether automatic brightness adjustment public static boolean IsAutoBrightness (Context context) {boolean IsAutoBrightness = false; try {IsAutoBrightness = Settings. system. getInt (context. getContentResolver (), Settings. system. SCREEN_BRIGHT NESS_MODE) = Settings. system. SCREEN_BRIGHTNESS_MODE_AUTOMATIC;} catch (SettingNotFoundException e) {e. printStackTrace ();} return IsAutoBrightness;} // obtain the brightness of the current screen. public static int getScreenBrightness (Context context) {int nowBrightnessValue = 0; ContentResolver resolver = context. getContentResolver (); try {nowBrightnessValue = android. provider. settings. system. getInt (resolver, Settings. system. SCREEN_BRIGH TNESS);} catch (Exception e) {e. printStackTrace ();} return nowBrightnessValue;} // sets the brightness. // after the program exits, the brightness becomes invalid. public static void setCurWindowBrightness (Context context, int brightness) {// If automatic brightness is enabled,. Otherwise, if (IsAutoBrightness (context) {stopAutoBrightness (context) ;}// context is converted to ActivityActivity activity = (Activity) context; WindowManager. layoutParams lp = activity. getWindow (). getAttributes (); // Exception Handling if (brightness <1) {brightness = 1;} // Exception Handling if (brightness> 255) {brightness = 255;} lp. screenBrightness = Float. valueOf (brightness) * (1f/255f); activity. getWindow (). setAttributes (lp);} // sets the system brightness. // after the program exits, the brightness is still valid. public static void setSystemBrightness (Context context, int brightness) {// Exception Handling if (brightness <1) {brightness = 1 ;}// Exception Handling if (brightness> 255) {brightness = 255 ;}savebrightness (context, brightness);} // stop automatic brightness adjustment public static void stopAutoBrightness (Context context) {Settings. system. putInt (context. getContentResolver (), Settings. system. SCREEN_BRIGHTNESS_MODE, Settings. system. SCREEN_BRIGHTNESS_MODE_MANUAL);} // enable brightness to automatically adjust public static void startAutoBrightness (Context context) {Settings. system. putInt (context. getContentResolver (), Settings. system. SCREEN_BRIGHTNESS_MODE, Settings. system. SCREEN_BRIGHTNESS_MODE_AUTOMATIC);} // Save the brightness setting state public static void saveBrightness (Context context, int brightness) {Uri uri = android. provider. settings. system. getUriFor ("screen_brightness"); android. provider. settings. system. putInt (context. getContentResolver (), "screen_brightness", brightness); context. getContentResolver (). policychange (uri, null );}}
Run
Project:
Http://download.csdn.net/detail/itas109/8706979
If you need to reprint please indicate the source: http://blog.csdn.net/itas109
QQ technology exchange group: 129518033