Android adjusts screen brightness implementation code _android

Source: Internet
Author: User

When adjusting the brightness of the screen, set the current activity brightness, and then save the brightness of the system.

1 onCreate () Check the system brightness and set the Seekbar:

     private void Screenbrightness_check () {///First turn off the system's brightness auto-adjust try {if (and Roid.provider.Settings.System.getInt (Getcontentresolver (), android.provider.Settings.System.SCREEN_BRIGHTNESS_ MODE) = = = Android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC) {Android . Provider. Settings.System.putInt (Getcontentresolver (), Android.provider.Settings.System.SCREEN_BRIGHTNESS_MO
              DE, Android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
              The catch (Settingnotfoundexception e) {//TODO auto-generated catch block
          E.printstacktrace (); //Gets the current brightness, and returns 255 intscreenbrightness= (int) (Android.provider.Settings.System.getInt Getcontentreso
          Lver (), Android.provider.Settings.System.SCREEN_BRIGHTNESS, 255)); Text, enterThe degree bar shows mseekbar_light.setprogress (intscreenbrightness);
         
     Mtextview_light.settext ("" +intscreenbrightness*100/255); }


2 Drag Seekbar to set the current activity brightness and save As System brightness:
         

Screen brightness private void setscreenbritness (int brightness) {//Do not allow screen full dark if (brightness<=1)
          {Brightness=1;
          //Set screen brightness for current activity windowmanager.layoutparams LP = This.getwindow (). GetAttributes (); 
          0 to 1, adjust brightness dark to full bright lp.screenbrightness = float.valueof (brightness/255f);
    
          This.getwindow (). SetAttributes (LP); Save As System brightness Method 1 Android.provider.Settings.System.putInt (Getcontentresolver (), ANDROID.PROVIDER.S Ettings.
         
          system.screen_brightness, brightness); 
Save As System brightness Method 2//URI URI = Android.provider.Settings.System.getUriFor ("screen_brightness");  
Android.provider.Settings.System.putInt (Getcontentresolver (), "screen_brightness", brightness); 
Resolver.registercontentobserver (URI, True, Mycontentobserver);
         
          Getcontentresolver (). Notifychange (URI, NULL); Change LightThe degree text displays Mtextview_light.settext ("" +brightness*100/255); }

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.