Question

Using the lockNow() function the display is turned off and then locked, but how to lock the display keeping it on? I'd like to know if exists a way to invoke the lock screen without turning the display off so the user can (eventually) unlock it without the necessity to press down the power button.

Was it helpful?

Solution

You can try to turn the screen on after the lockNow() call with something like that:

PowerManager powerManager = ((PowerManager) getSystemService(Context.POWER_SERVICE));
WakeLock screenLock = powerManager.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "tag");
screenLock.acquire();
screenLock.release();

You will need to add the required permission to the manifest file:

<uses-permission android:name="android.permission.WAKE_LOCK" />

OTHER TIPS

There's also another way to lock screen. Try that one. It may work differently and not turn of the screen (I can't say exactly, you can check it). Instead of using lockNow(), you can try to utilize KeyguardManager. You can manipulate your lockscreen by changing keyguard properties.

Add required permission to the manifest file:

<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />

Then you can try something like this:

KeyguardManager keyguardManager = (KeyguardManager)getSystemService(Activity.KEYGUARD_SERVICE);
KeyguardLock l = keyguardManager.newKeyguardLock(KEYGUARD_SERVICE);
l.reenableKeyguard();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top