Search code examples
androidandroid-windowmanagerscreen-brightness

Setting screenBrightness attribute to 0.0f in Android 4.2.2 no longer turns screen off?


Here's the relevant code:

 WindowManager.LayoutParams windowParams = getWindow().getAttributes();
 windowParams.flags |= LayoutParams.FLAG_KEEP_SCREEN_ON;
 windowParams.screenBrightness = 0.0f;
 getWindow().setAttributes(windowParams);

I also tried setting screenBrightness to 0 (an integer rather than float), as well as the following line I found in a Stack Overflow answer:

this.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

No dice. The screen dims, but does not turn off. The above code worked in previous Android versions. I just tested it in an emulator to make sure. Was a new method implemented to control the screen?


Solution

  • From what I have found, it is no longer possible to reliably turn the screen off in newer version of Android. The only solution which appears to work is the one which requires the DEVICE_POWER permission, which is a restricted permission.