5

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.

I have already tried to store the old value when the app starts and repeatedly check usingSettings.System.getInt(getContentResolver(),Settings.System.SCREEN_BRIGHTNESS); to compare the initial and final value of screen brightness , which is not a good way of doing so.

Thanks

EDIT: This question states that I have already tried the solution of using Settings.System.SCREEN_BRIGHTNESS to get current screen values and periodically check for screen brightness changes. I am looking for a more efficient way of doing such an operation.

Irfan S
  • 267
  • 5
  • 14
  • 1
    Possible duplicate of [Get preferred screen brightness in Android](https://stackoverflow.com/questions/4544967/get-preferred-screen-brightness-in-android) – Michael Sep 08 '17 at 15:03
  • 1
    There doesn't seem to be any system level broadcast about the screen brightness so you cannot know when the user changed it unless you keep polling, which I agree is not the best practice. Add to it the issue that @Michael just linked and you got a pretty hard problem in your hands. – Edson Menegatti Sep 08 '17 at 15:04
  • @Michael the problem being discussed there suggests the same method I have already tried, I was looking for an alternative solution :D – Irfan S Sep 08 '17 at 15:28

2 Answers2

12

yes, there is a way by using ContentObserver:

  • code:

      // listen to the brightness system settings
      val contentObserver = object:ContentObserver(Handler())
      {
          override fun onChange(selfChange:Boolean)
          {
              // get system brightness level
              val 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:

Eric
  • 16,397
  • 8
  • 68
  • 76
5

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.

Bhuvanesh BS
  • 13,474
  • 12
  • 40
  • 66
  • I have already tried out this method (already stated in question) , was looking for a more efficient way to do so. – Irfan S Sep 08 '17 at 15:29
  • 1
    The answer is no. There is no method to do it efficiently. See the link I have shared in the answer. – Bhuvanesh BS Sep 08 '17 at 15:31