Various methods related to screen brightness adjustment

Source: Internet
Author: User

Public class SystemManager {private Context mContext; private static SystemManager sInstance; private SystemManager (final Context context) {mContext = context;} public static SystemManager init (final Context context) {if (null = sInstance) {sInstance = new SystemManager (context);} return sInstance;} public static SystemManager getInstance () {return sInstance ;} // determine whether the automatic dimming mode is public boolean isAutoBrightness () {boolean automicBrightness = false; try {ContentResolver resolver = mContext. getContentResolver (); automicBrightness = Settings. system. getInt (resolver, Settings. system. SCREEN_BRIGHTNESS_MODE) = Settings. system. SCREEN_BRIGHTNESS_MODE_AUTOMATIC;} catch (SettingNotFoundException e) {e. printStackTrace ();} return automicBrightness;} // set the screen brightness public void setBrightness (Activity activity, int brightness) {WindowManager. layoutParams lp = activity. getWindow (). getAttributes (); lp. screenBrightness = Float. valueOf (brightness) * (1f/255f); activity. getWindow (). setAttributes (lp);} // Save the screen brightness public void saveBrightness (int brightness) {ContentResolver resolver = mContext. getContentResolver (); Uri uri = android. provider. settings. system. getUriFor ("screen_brightness"); android. provider. settings. system. putInt (resolver, "screen_brightness", brightness); resolver. policychange (uri, null);} // Enable Automatic dimming mode public void startAutoBrightness () {ContentResolver resolver = mContext. getContentResolver (); Settings. system. putInt (resolver, Settings. system. SCREEN_BRIGHTNESS_MODE, Settings. system. SCREEN_BRIGHTNESS_MODE_AUTOMATIC); Uri uri = android. provider. settings. system. getUriFor ("screen_brightness"); resolver. policychange (uri, null);} // disable the automatic dimming mode public void stopAutoBrightness () {ContentResolver resolver = mContext. getContentResolver (); Settings. system. putInt (resolver, Settings. system. SCREEN_BRIGHTNESS_MODE, Settings. system. SCREEN_BRIGHTNESS_MODE_MANUAL); Uri uri = android. provider. settings. system. getUriFor ("screen_brightness"); resolver. policychange (uri, null);} // obtain the current screen brightness. public int getScreenBrightness () {int nowBrightnessValue = 0; try {ContentResolver resolver = mContext. getContentResolver (); nowBrightnessValue = android. provider. settings. system. getInt (resolver, Settings. system. SCREEN_BRIGHTNESS);} catch (Exception e) {e. printStackTrace ();} return nowBrightnessValue;} // set public void setBrightnessMode (int mode) {Settings. system. putInt (mContext. getContentResolver (), Settings. system. SCREEN_BRIGHTNESS_MODE, mode);} // obtain the brightness mode public int getBrightnessMode () {try {return Settings. system. getInt (mContext. getContentResolver (), Settings. system. SCREEN_BRIGHTNESS_MODE);} catch (SettingNotFoundException e) {return Settings. system. SCREEN_BRIGHTNESS_MODE_AUTOMATIC ;}}}


Permission

<Uses-permission android: name = "android. permission. WRITE_SETTINGS"/>



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.