I am writing an application to manage or customize the unlock screen of the android devices. It works in the following steps:
- The user locks the screen using the power button.
- User attempts to unlock the screen, thus pressing the power button again
- My activity pops up -- The screen is still locked
- The user answers a question and if the answer is correct the screen unlocks
I have created an activity for the third step and added the following code to its onCreate
method:
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
window.addFlags(WindowManager.LayoutParams.FLAG_SECURE);
This works properly and exactly as I had hoped. My problem is with the fourth step. I have searched and found a number of solutions, but none of them are doing it for me.
How to lock/unlock phone programmatically -- This is just a way to show an activity on top of the lock screen. It does not unlock it.
Android screen lock/ unlock programatically -- This solution is not only deprecated but it's also really unreliable. It does not unlock the screen, but creates another lock and unlocks that. So using this you can access the applications but it's impossible to access the sensitive settings. (Try it!)
I tried registering my application as a device administrator. That didn't help me as well. I can change the setting but I cannot unlock the screen.