I'm working on an app that has call capabilities so I need the device to wake up. If the device is locked, I use the following code to unlock it:
keyguardManager.requestDismissKeyguard(this, new KeyguardManager.KeyguardDismissCallback() {
@Override
public void onDismissError() {
super.onDismissError();
AppLog.d(TAG, "Inside onDismissError()");
}
@Override
public void onDismissSucceeded() {
super.onDismissSucceeded();
AppLog.d(TAG, "Inside onDismissSucceeded()");
}
@Override
public void onDismissCancelled() {
super.onDismissCancelled();
AppLog.d(TAG, "Inside onDismissCancelled()");
}
});
On an initial call, the device is unlocking without any issues, but on a second call, there is a delay from when requestDismissKeyguard is called and when the callback is called, so my device stays on with the lock screen open, causing a weird state because the user is expecting an incoming call screen.
See my logs here:
2021-04-21 16:25:39.885: Invoking requestDismissKeyguard()
2021-04-21 16:25:39.994: Inside onDismissCancelled()
2021-04-21 16:25:51.835: Invoking requestDismissKeyguard()
2021-04-21 16:25:55.311: Inside onDismissCancelled()
Notice the difference between the first call (gets canceled within 100ms) and the second call (gets canceled within 4s!!!)
Any ideas? Suggestions?
I assume this code block is in an activity so, I'm not entirely sure why you're trying to request it via keyguard manager (though it looks like it's a proper way after checking the documentation.) Maybe try this:
After calling
setContentView
you can callgetWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED)
to remove keyguard temporarily for your activity. Finishing the activity should return the keyguard back.The solution for this follows: