Android brightness adjustment tool: BrightnessUtils; android brightness adjustment

Source: Internet
Author: User

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

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.