Android adjusts screen brightness

Source: Internet
Author: User

Android screen brightness seems to be in the 2.1+ to provide automatic adjustment function, so, if when the automatic adjustment function, we adjust as if there is no effect, only to determine whether to turn on the screen brightness automatic adjustment function, if the need to turn off.

 PackageCom.vm.common;Importandroid.app.Activity;ImportAndroid.content.ContentResolver;ImportAndroid.net.Uri;Importandroid.provider.Settings;Importandroid.provider.Settings.SettingNotFoundException;ImportAndroid.util.Log;ImportAndroid.view.WindowManager; Public classBrightnesstools {/*** Determine if automatic brightness adjustment is turned on*/     Public Static Booleanisautobrightness (Contentresolver acontentresolver) {BooleanAutomicbrightness =false; Try{automicbrightness=Settings.System.getInt (Acontentresolver, Settings.System.SCREEN_BRIGHTNESS_MODE)==Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC; }Catch(settingnotfoundexception e) {e.printstacktrace (); }        returnautomicbrightness; }    /*** Get the brightness of the screen *@paramactivity *@return     */     Public Static intgetscreenbrightness (activity activity) {intNowbrightnessvalue = 0; Contentresolver Resolver=Activity.getcontentresolver (); Try{Nowbrightnessvalue=android.provider.Settings.System.getInt (resolver, Settings.System.SCREEN_BRIGHTNESS); }Catch(Exception e) {e.printstacktrace (); }        returnNowbrightnessvalue; }    /*** Set Brightness *@paramactivity *@paramBrightness*/     Public Static voidSetbrightness (activity activity,intbrightness) {        //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.screenbrightness = =" +lp.screenbrightness);    Activity.getwindow (). SetAttributes (LP); }    //So, can set up, but why still appear, set up, not reflected? //hey, that's because, turn on the automatic adjustment function, then how to close it? This is the most important thing:    /*** Stop automatic brightness adjustment *@paramActivity*/     Public Static voidstopautobrightness (activity activity) {Settings.System.putInt (Activity.getcontentresolver (), Settings.    System.screen_brightness_mode, Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL); }    /*** Turn on brightness auto Adjust *@paramActivity*/     Public Static voidstartautobrightness (activity 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 operating brightness of almost all have, the end! //Alas, it was supposed to be over, but the tragedy is, since just like that, only in the current activity has a role, a period of withdrawal, will find no role, tragedy, the original is forgotten to save. Sweat!     /*** Save Brightness setting status *@paramResolver *@paramBrightness*/     Public Static voidSavebrightness (Contentresolver Resolver,intbrightness) {URI Uri=Android.provider.Settings.System. Geturifor ("Screen_brightness"); Android.provider.Settings.System.putInt (Resolver,"Screen_brightness", brightness); //Resolver.registercontentobserver (URI, True, mycontentobserver);Resolver.notifychange (URI,NULL); }}

Test

 //set the brightness in the app not to save     Public voidBrightness (view view) {BooleanAutobrightness =brightnesstools.isautobrightness (Getcontentresolver ()); if(autobrightness) {T.showlong ( This, "Turn off auto Adjust brightness"); Brightnesstools.stopautobrightness ( This); } brightnesstools.setbrightness ( This, 255); T.showlong ( This, "brightness is set to maximum"); }    //gets the current brightness and sets the maximum brightness save     Public voidsavebrightness (view view) {intScreenbrightness = Brightnesstools.getscreenbrightness ( This); T.showlong ( This, "Current screen brightness is:" +screenbrightness); Brightnesstools.savebrightness (Getcontentresolver (),255); }    //Turn off auto Adjust brightness     Public voidOffauto (view view) {BooleanAutobrightness =brightnesstools.isautobrightness (Getcontentresolver ()); if(autobrightness) {T.showlong ( This, "Turn off auto Adjust brightness"); Brightnesstools.stopautobrightness ( This); }Else{T.showlong ( This, "Turn on auto Adjust brightness"); Brightnesstools.startautobrightness ( This); }    }



Android adjusts screen brightness

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.