0

It's pretty easy to set the autoBrightness in Android, isn't it?

Settings.System.putInt(resolver,
        Settings.System.SCREEN_BRIGHTNESS_MODE, 0); // 0 is manual, 1 is auto

But if you are toggling:

boolean isAuto = Settings.System.getInt(resolver, 
        Settings.System.SCREEN_BRIGHTNESS_MODE) == 1;
Settings.System.putInt(resolver,
        Settings.System.SCREEN_BRIGHTNESS_MODE, isAuto ? 0 : 1);

It won't automatically adapt until you lock the screen and lock it again.

zx81
  • 41,100
  • 9
  • 89
  • 105
Twinone
  • 2,949
  • 4
  • 28
  • 39
  • What about putting `isAuto ? 0 : 1` in its own () maybe...? – TronicZomB Mar 18 '13 at 12:07
  • Not too sure if that would make a difference or not. – TronicZomB Mar 18 '13 at 12:17
  • isAuto?0:1 is a ternary (boolean) expresion, it will be exactly the same with or without parentheses, but it's cleaner with. I was just writing quickly, as this is a question-answer i wanted to share because it was driving me crazy for weeks :-) – Twinone Mar 18 '13 at 12:18
  • Ok, I know the expresion but I wasn't sure if the () made a difference. It does seem odd that it wouldn't work the way you had it in your question... – TronicZomB Mar 18 '13 at 12:24
  • It actually sets the setting right in the settings menu, if you monitor it, you can see that the setting is changed. However, the brightness is not adapted instantly, instead you must lock and unlock the screen to take effect. Putting it to manual again before auto, causes it to apply directly (don't quite know why) – Twinone Mar 18 '13 at 12:38
  • Interesting... I'll keep that in mind. Thanks for posting this. – TronicZomB Mar 18 '13 at 12:41

1 Answers1

0

I don't know why but setting it again to 0 before setting it to 1 works:

boolean isAuto = Settings.System.getInt(resolver, 
    Settings.System.SCREEN_BRIGHTNESS_MODE) == 1;
if (isAuto) {
    Settings.System.putInt(resolver,
        Settings.System.SCREEN_BRIGHTNESS_MODE, 0); // set to 0 again, even if it is already 0
    Settings.System.putInt(resolver,
        Settings.System.SCREEN_BRIGHTNESS_MODE, 1); // now it auto-adapts
} else {
    Settings.System.putInt(resolver,
         Settings.System.SCREEN_BRIGHTNESS_MODE, 0);
}
Twinone
  • 2,949
  • 4
  • 28
  • 39