Skip to content Skip to sidebar Skip to footer

How To Detect If Screen Brightness Has Changed In Android?

I have searched extensively and couldn't find a similar question. I would like to know if there is any way to detect when the screen brightness of a mobile device has been changed.

Solution 1:

yes, there is a way by using ContentObserver:

  • code:

    // listen to the brightness system settingsval contentObserver = object:ContentObserver(Handler())
      {
          overridefunonChange(selfChange:Boolean)
          {
              // get system brightness levelval brightnessAmount = Settings.System.getInt(
                      contentResolver,Settings.System.SCREEN_BRIGHTNESS,0)
    
              // do something...
          }
      }
    
      // register the brightness listener upon starting
      contentResolver.registerContentObserver(
              Settings.System.getUriFor(Settings.System.SCREEN_BRIGHTNESS),
              false,contentObserver)
    
      // .....// unregister the listener when we're done (e.g. activity destroyed)
      contentResolver.unregisterContentObserver(contentObserver)
    

other useful links:

Solution 2:

There are no receivers provided to detect brightness change.

You have to run a Service or Thread to check the brightness change by yourself.

Settings.System.getInt(getContext().getContentResolver(), 
             Settings.System.SCREEN_BRIGHTNESS);

The above code will give you current system brightness level. Periodically detect the brightness and compare with the old one.

Note: If the system is in Auto Brightness mode, you can't get current brightness level. See this answer.

Post a Comment for "How To Detect If Screen Brightness Has Changed In Android?"