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

Here is 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 the ScreenBrightness parameter to 0 (an integer, not a float), as well as the following line found in the "Stack Overflow" answer:

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

No dice. The screen lights up but does not turn off. The above code worked in previous versions of Android. I just tested it in an emulator to make sure. Has a new screen management method been implemented?

+6
source share
4 answers

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

0
source

Please call the following two functions to turn off the screen, as your code is incompatible.

From Documents:

  public void goToSleep (long time) 

Added to API level 1. Forces the device to enter sleep mode.

Overrides all tracking locks that are stored. This is what happens when you press the power key to turn off the screen. Requires DEVICE_POWER permission.

  public void wakeUp (long time) 

Added to API Level 17 Causes the device to wake up from sleep.

If the device is currently sleeping, wakes up, otherwise nothing happens. This is what happens when you press the power key to turn on the screen.

Requires DEVICE_POWER permission.

0
source

I'm not sure why what you are doing is not working. This is a dirty hack, but perhaps you can change the screen timeout at a very low time.

 android.provider.Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT, time); 

Say time=300 , which is 300 milliseconds.

0
source

I don’t know exactly what you are trying to do here, but specifically for screen control such as wake / sleep, you should take a look at the PowerManager class, which is simple and convenient to use: http://developer.android.com/reference/android/ os / PowerManager.html

This is an example of how to use it:

 protected void setScreenLock(boolean on){ if(mWakeLock == null){ PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); mWakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ON_AFTER_RELEASE, TAG); } if(on){ mWakeLock.acquire(); }else{ if(mWakeLock.isHeld()){ mWakeLock.release(); } mWakeLock = null; } } 

Hello!

0
source

Source: https://habr.com/ru/post/946833/


All Articles