Skip to content

Instantly share code, notes, and snippets.

@Jeevuz
Created September 20, 2016 10:41
Show Gist options
  • Save Jeevuz/4ec01688083670b1f3f92af64e44c112 to your computer and use it in GitHub Desktop.
Save Jeevuz/4ec01688083670b1f3f92af64e44c112 to your computer and use it in GitHub Desktop.
Check device is currently locked
/**
* Returns true if the device is locked or screen turned off (in case password not set)
*/
public static boolean isDeviceLocked(Context context) {
boolean isLocked = false;
// First we check the locked state
KeyguardManager keyguardManager = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
boolean inKeyguardRestrictedInputMode = keyguardManager.inKeyguardRestrictedInputMode();
if (inKeyguardRestrictedInputMode) {
isLocked = true;
} else {
// If password is not set in the settings, the inKeyguardRestrictedInputMode() returns false,
// so we need to check if screen on for this case
PowerManager powerManager = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
isLocked = !powerManager.isInteractive();
} else {
//noinspection deprecation
isLocked = !powerManager.isScreenOn();
}
}
Loggi.d(String.format("Now device is %s.", isLocked ? "locked" : "unlocked"));
return isLocked;
}
@l0ksh
Copy link

l0ksh commented Aug 4, 2018

what is "Loggi " here ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment