SoFunction
Updated on 2025-04-07

Android adjusts screen brightness implementation code

When adjusting the screen brightness, set the current activity brightness first, and then save it as system brightness.

1 Check the system brightness in onCreate() and set seekBar:

     private void screenBrightness_check()
     {
          //First turn off the system's brightness automatic adjustment          try
          {
              if((getContentResolver(),.SCREEN_BRIGHTNESS_MODE) == .SCREEN_BRIGHTNESS_MODE_AUTOMATIC)
              {    
                   (getContentResolver(),
                        .SCREEN_BRIGHTNESS_MODE,
                        .SCREEN_BRIGHTNESS_MODE_MANUAL);
              }
          }
          catch (SettingNotFoundException e)
          {
              // TODO Auto-generated catch block
              ();
          }
          //Get the current brightness, if the acquisition fails, return 255          intScreenBrightness=(int)((getContentResolver(),
                   .SCREEN_BRIGHTNESS,
                   255));
          //Text and progress bar display          mSeekBar_light.setProgress(intScreenBrightness);
          mTextView_light.setText(""+intScreenBrightness*100/255);
         
     }


2 When dragging seekBar, set the current activity brightness and save it as system brightness:
        

//Screen Brightness     private void setScreenBritness(int brightness)
     {
          //Don't let the screen be completely dark          if(brightness<=1)
          {
              brightness=1;
          }
          //Set the screen brightness of the current activity           lp = ().getAttributes();
          //0 to 1, adjust the brightness to full brightness           = (brightness/255f); 
          ().setAttributes(lp);
    
          //Save as system brightness method 1          (getContentResolver(),
                   .SCREEN_BRIGHTNESS,
                   brightness);
         
          //Save as system brightness method 2//        Uri uri = ("screen_brightness"); 
//        (getContentResolver(), "screen_brightness", brightness);  
//        // (uri, true, myContentObserver); 
//        getContentResolver().notifyChange(uri, null);
         
          //Change brightness text display          mTextView_light.setText(""+brightness*100/255);
     }