DevicePolicyManager.lockNow(); doesn't turn off screen when security settings are set to Slide/None

android, device-admin

Solution

I used Delyan's comment to achieve this:

If you request WRITE_SETTINGS, you can probably set the screen timeout to be 1 sec - android.provider.Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT, 1000); Of course, then you're messing up the user's settings but I can't figure out a better way to do it. I have seen apps that do it - NoLED is the most prominent example.

Problem

The user expects my app to switch off the screen after being used. At the moment, I achieve this with Device Administrator rights and `DevicePolicyManager.lockNow()`, which works fine if the security settings are set to PIN/Pattern/FaceUnlock, etc. However, if using Slide/None, the above command just takes the user to the homescreen (or doesn't do anything), which is understandable, since there's nothing to "lock". Is there any way to achieve turning off the screen in such a situation? My app requires SDK>=16, if that matters. So I guess my question is: How can an app reliably switch off the screen (I'm not holding a `wakelock`, I'm using the `WindowManager`-flags `FLAG_TURN_SCREEN_ON` in `onAttachedToWindow()`). The "flow" of my app is: - Activity is started by an intent while the screen is off, shows above the keyguard/switches on the screen with the above-mentioned flags - User actively dismisses my Activity, I'm calling `lockNow()` and `finish()` and the user expects the screen to turn off. If the user is using the none/slide lock, this doesn't work and instead the user's homescreen is shown Thanks!

Original source

Related problems