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

4

6

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?

Malynda answered 8/6, 2013 at 4:19 Comment(3)
can i get a clarification regarding your question.. i have been looking at the change logs in api version 17. I dont seem to find any change in the way android is interpreting your code. Power manager is the recommended way to switch off your screen .Fervid
I'm not aware that this ever worked reliably across devices.Spindry
Why do you even need to lock the screen, or for that matter turn the screen off programmatically? I just don't think that it's necessary to have to lock the screen for someone. At any rate, there doesn't seem to be an Official way to lock the screen other than... pushing the button... It's likely Google doesn't want developers to have access to things like locking the screen for security reasons. It would be really easy to write a bit of malware that runs in the background and randomly locks the screen.Crankle
M
0

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.

Malynda answered 1/7, 2014 at 19:39 Comment(0)
F
0

Please call the following two functions to switch off your screen as your code is not consistent.

From Docs:

 public void goToSleep (long time)

Added in API level 1 Forces the device to go to sleep.

Overrides all the wake locks that are held. This is what happens when the power key is pressed to turn off the screen. Requires the DEVICE_POWER permission.

 public void wakeUp (long time)

Added in API level 17 Forces the device to wake up from sleep.

If the device is currently asleep, wakes it up, otherwise does nothing. This is what happens when the power key is pressed to turn on the screen.

Requires the DEVICE_POWER permission.

Fervid answered 8/6, 2013 at 5:49 Comment(1)
The DEVICE_POWER permission is restricted to specially signed applications, specifically system apps. Thanks for responding.Malynda
M
0

I'm not sure why what you're doing isn't working. This is a dirty hack but perhaps you could change the screen timeout to a very low time.

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

Say time=300 which is 300 milliseconds.

Methodist answered 27/6, 2013 at 17:21 Comment(0)
P
0

I don't know exactly what you are trying to accomplish here, but definitely for screen controlling like wake/sleep, you should take a look at the PowerManager class, is simple and easy 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;
    }

}

Regards!

Pocket answered 1/7, 2013 at 18:25 Comment(0)
M
0

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.

Malynda answered 1/7, 2014 at 19:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.