6

I need to change the screen brightness programmatically. I read multiple solutions like this Can't apply system screen brightness programmatically in Android.

My problem is that those solutions implies changing the activity to be effective (having something like a dummy activity finishing immediately) and I would like to avoid the overhead of an activity switch.

Is there any other solution... maybe using native code so that the screen brightness will change immediately ?

Community
  • 1
  • 1
ben75
  • 29,217
  • 10
  • 88
  • 134

1 Answers1

7

The following affects immediately the single activity, no need to restart it. The activity also remembers the screenBrightness attribute over pause/resume.

WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.screenBrightness = 1; // 0f - no backlight ... 1f - full backlight
getWindow().setAttributes(lp);

But it has no effect if you have automatic backlight level enabled in the system settings. This solution should help to turn off automatic backlight.

Community
  • 1
  • 1
petrch
  • 532
  • 3
  • 6
  • Setting the screen brightness using WindowManager.LayoutParams works even if "automatic backlight level" IS enabled. As stated in the documentation, "screenBrightness can be used to override the user's preferred brightness of the screen". Tested on Android 4.1 and 4.4. Here is a confirmation from an Android framework engineer: https://groups.google.com/forum/#!msg/android-developers/tYlCn7bvAmw/2quRf_ZZZigJ – TechAurelian May 06 '14 at 10:27