1

In Android user can change brightness by "rolling down" the status bar (or notification bar, whatever this is called in Android) and changing brightness there.

I need to display brightness slider in my app. I know how to get system brightness value, but the question is how to know if user changed the brightness? When user rolls-up the notification bar I don't get onResume. How do I know that the brightness has changed or the notification bar revealed my activity to the user?

Łukasz Sromek
  • 3,567
  • 3
  • 29
  • 43
  • https://stackoverflow.com/questions/46119279/how-to-detect-if-screen-brightness-has-changed-in-android – user9170 Oct 07 '20 at 14:48

2 Answers2

3

Try creating a thread with a runnable that runs indefinitely (or till you want it to run) where you listen for a change in the current brightness. This can be done pretty easily

  1. Find the original screen brightness and assign it to a variable called oldScreenBrightness.

  2. In your thread check

    currentScreenBrightness = getCurrentScreenBrightness();
    if (oldScreenBrightness != currentScreenBrightness)
    {
        doSomething()
        oldScreenBrightness = currentScreenBrightness
    }
    else
    {
       doNothing or doSomethingElse
    }
    
Lucifer
  • 28,933
  • 23
  • 88
  • 140
Vishwa Patel
  • 2,681
  • 1
  • 16
  • 16
  • 1
    my god, is it the only way? when posting my question i was about to write "and please don't tell me i have to resort to polling" :) android is ridiculous :( – Łukasz Sromek Aug 08 '12 at 07:09
  • I've looked into this issue in the past and after much research I came to the conclusion that this was the only solution. Android is actually pretty amazing but yours in a an extremely niche case I'd say. – Vishwa Patel Aug 08 '12 at 10:15
  • after two years of programming for iPhone and one year Android I have my own opinion about the latter ;) thanks for the answer, I'll accept it after a few days if nobody comes up with some more event-based solution. – Łukasz Sromek Aug 08 '12 at 10:38
  • haha very well then, I think I should try out iPhone dev, now only if Objective-C didn't look so messy. – Vishwa Patel Aug 08 '12 at 10:57
  • yes, I interned there over the last two months. I'm sorry but who are you? – Vishwa Patel Aug 08 '12 at 11:30
  • Yes, I've just finished my 1st year of Bachelor of Engineering and Computer Science. – Vishwa Patel Aug 17 '12 at 11:14
-1

Go to settings Select display Select notification panel Check Brightness adjustment