Reputation: 9507
How can I check if the screen is unlocked (E.i. Turned on and not on lockscreen)?
PS. I'm not looking for the unlock event, which I know can be retrieved with an AdminDeviceReceiver, but I'm looking for an executable code that will return a boolean telling me whether or not the screen is unlocked.
Upvotes: 4
Views: 11822
Reputation: 2060
As per google Doc inKeyguardRestrictedInputMode
is Deprecated. Instead of You can use below method for phone is wake up or not.
private boolean isPhoneIsLockedOrNot(Context context) {
boolean isPhoneLock = false;
if (context != null) {
KeyguardManager myKM = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
if (myKM != null && myKM.isKeyguardLocked()) {
isPhoneLock = true;
}
}
return isPhoneLock;
}
Upvotes: 1
Reputation: 1023
Check the Screen Off State in your onPause Method using Power Manager
@Override
protected void onPause()
{
super.onPause();
// If the screen is off then the device has been locked
PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
boolean isScreenOn = powerManager.isScreenOn();
if (!isScreenOn) {
// The screen has been locked
// do stuff...
}
}
Upvotes: 2
Reputation: 2943
Try with this:
KeyguardManager myKM = (KeyguardManager) сontext.getSystemService(Context.KEYGUARD_SERVICE);
if( myKM.inKeyguardRestrictedInputMode() ) {
// it is locked
} else {
//it is not locked
}
Taken from: Detecting when screen is locked
Also same answer : How to tell if user is on lock screen from service
Upvotes: 18
Reputation: 53687
KeyguardManager keyguardManager = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
if( keyguardManager.inKeyguardRestrictedInputMode()) {
System.out.println("~~~SCREEN LOCKED~~~");
} else {
System.out.println("~~~SCREEN NOT LOCKED~~~");
}
Upvotes: 4