Android screen brightness adjustment

Source: Internet
Author: User
Package com. sensor. Demo;
/*** From http://apps.hi.baidu.com/share/detail/53623456 */import android. app. activity; import android. content. contentresolver; import android.net. uri; import android. provider. settings; import android. provider. settings. settingnotfoundexception; import android. util. log; import android. view. windowmanager; public class brightnesstools {/* the screen brightness of Android seems to have provided the automatic adjustment function at 2.1 +. * therefore, if the automatic adjustment function is enabled, there seems to be nothing to adjust. Yes. * This makes me speechless. The result is only a judgment to see if the automatic brightness adjustment function is enabled. * // *** Determines whether automatic brightness adjustment is enabled */public static Boolean isautobrightness (contentresolver acontentresolver) {Boolean automicbrightness = false; try {automicbrightness = settings. system. getint (acontentresolver, settings. system. screen_brightness_mode) = settings. system. screen_brightness_mode_automatic;} catch (settingnotfoundexception e) {e. printstacktrace ();} return automicbrightness;} // The brightness of the current image. Tangled:/*** get screen brightness */public static int getscreenbrightness (activity) {int nowbrightnessvalue = 0; contentresolver resolver = activity. getcontentresolver (); try {nowbrightnessvalue = android. provider. settings. system. getint (Resolver, settings. system. screen_brightness);} catch (exception e) {e. printstacktrace ();} return nowbrightnessvalue;} // how can I modify the screen brightness? /*** Set brightness */public static void setbrightness (activity, int brightness) {// settings. system. putint (activity. getcontentresolver (), // settings. system. screen_brightness_mode, // settings. system. screen_brightness_mode_manual); windowmanager. layoutparams Lp = activity. getwindow (). getattributes (); LP. screenbrightness = float. valueof (brightness) * (1f/255f); log. D ("lxy", "set LP. screenbrightn ESS = "+ LP. screenbrightness); activity. getwindow (). setattributes (LP) ;}// you can set it, but why is it still displayed? If it is set, it does not reflect it? // Hey, that's because the automatic adjustment function is enabled. How can I disable it? This is the most important thing:/*** stop automatic brightness adjustment */public static void stopautobrightness (activity) {settings. system. putint (activity. getcontentresolver (), settings. system. screen_brightness_mode, settings. system. screen_brightness_mode_manual);} // if it can be enabled, it should be able to close the handler. How can it be disabled? Very simple:/*** enable automatic brightness adjustment ** @ Param activity */public static void startautobrightness (activity) {settings. system. putint (activity. getcontentresolver (), settings. system. screen_brightness_mode, settings. system. screen_brightness_mode_automatic);} // at this point, it should be said that the operation brightness is almost all there, and it is over! // Ah, I thought it should be over, but the tragedy is that, since it is set as just now, it can only play a role in the current activity, we will find that there is no effect, and the tragedy is that we forgot to save it. Khan! /*** Save brightness setting status */public static void savebrightness (contentresolver resolver, int brightness) {URI uri = android. provider. settings. system. geturifor ("screen_brightness"); android. provider. settings. system. putint (Resolver, "screen_brightness", brightness); // resolver. registercontentobserver (Uri, true, mycontentobserver); resolver. policychange (Uri, null );}}
/*** From http://apps.hi.baidu.com/share/detail/53623456 */import android. app. activity; import android. content. contentresolver; import android.net. uri; import android. provider. settings; import android. provider. settings. settingnotfoundexception; import android. util. log; import android. view. windowmanager; public class brightnesstools {/* the screen brightness of Android seems to have provided the automatic adjustment function at 2.1 +. * therefore, if the automatic adjustment function is enabled, there seems to be nothing to adjust. Yes. * This makes me speechless. The result is only a judgment to see if the automatic brightness adjustment function is enabled. * // *** Determines whether automatic brightness adjustment is enabled */public static Boolean isautobrightness (contentresolver acontentresolver) {Boolean automicbrightness = false; try {automicbrightness = settings. system. getint (acontentresolver, settings. system. screen_brightness_mode) = settings. system. screen_brightness_mode_automatic;} catch (settingnotfoundexception e) {e. printstacktrace ();} return automicbrightness;} // The brightness of the current image. Tangled:/*** get screen brightness */public static int getscreenbrightness (activity) {int nowbrightnessvalue = 0; contentresolver resolver = activity. getcontentresolver (); try {nowbrightnessvalue = android. provider. settings. system. getint (Resolver, settings. system. screen_brightness);} catch (exception e) {e. printstacktrace ();} return nowbrightnessvalue;} // how can I modify the screen brightness? /*** Set brightness */public static void setbrightness (activity, int brightness) {// settings. system. putint (activity. getcontentresolver (), // settings. system. screen_brightness_mode, // settings. system. screen_brightness_mode_manual); windowmanager. layoutparams Lp = activity. getwindow (). getattributes (); LP. screenbrightness = float. valueof (brightness) * (1f/255f); log. D ("lxy", "set LP. screenbrightn ESS = "+ LP. screenbrightness); activity. getwindow (). setattributes (LP) ;}// you can set it, but why is it still displayed? If it is set, it does not reflect it? // Hey, that's because the automatic adjustment function is enabled. How can I disable it? This is the most important thing:/*** stop automatic brightness adjustment */public static void stopautobrightness (activity) {settings. system. putint (activity. getcontentresolver (), settings. system. screen_brightness_mode, settings. system. screen_brightness_mode_manual);} // if it can be enabled, it should be able to close the handler. How can it be disabled? Very simple:/*** enable automatic brightness adjustment ** @ Param activity */public static void startautobrightness (activity) {settings. system. putint (activity. getcontentresolver (), settings. system. screen_brightness_mode, settings. system. screen_brightness_mode_automatic);} // at this point, it should be said that the operation brightness is almost all there, and it is over! // Ah, I thought it should be over, but the tragedy is that, since it is set as just now, it can only play a role in the current activity, we will find that there is no effect, and the tragedy is that we forgot to save it. Khan! /*** Save brightness setting status */public static void savebrightness (contentresolver resolver, int brightness) {URI uri = android. provider. settings. system. geturifor ("screen_brightness"); android. provider. settings. system. putint (Resolver, "screen_brightness", brightness); // resolver. registercontentobserver (Uri, true, mycontentobserver); resolver. policychange (Uri, null );}}
Related Article

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.