When screen is blocked in Android?
-
08-07-2021 - |
Question
Does anybody know easiest way to determine, when device's screen is blocked (not switched off)?
It is necessary for the case, when notifications may contain private information, but they shown on blocked screen. I want not to show it.
Solution
if(isKeyboardLocked(context)){
// screen blocked
}
private boolean isKeyboardLocked(Context context) {
KeyguardManager myKM = (KeyguardManager) context
.getSystemService(Context.KEYGUARD_SERVICE);
return myKM.inKeyguardRestrictedInputMode();
}
OTHER TIPS
This is the way to check it
KeyguardManager myKM = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
if( myKM.inKeyguardRestrictedInputMode()) {
//it is locked
} else {
//it is not locked
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow