Created
September 20, 2016 10:41
-
-
Save Jeevuz/4ec01688083670b1f3f92af64e44c112 to your computer and use it in GitHub Desktop.
Check device is currently locked
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
what is "Loggi " here ?