-
-
Save doridori/54c32c66ef4f4e34300f to your computer and use it in GitHub Desktop.
Check if a device lock is set on Android. Code example for http://stackoverflow.com/a/27801128/236743
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
/** | |
* <p>Checks to see if the lock screen is set up with either a PIN / PASS / PATTERN</p> | |
* | |
* <p>For Api 16+</p> | |
* | |
* @return true if PIN, PASS or PATTERN set, false otherwise. | |
*/ | |
public static boolean doesDeviceHaveSecuritySetup(Context context) | |
{ | |
return isPatternSet(context) || isPassOrPinSet(context); | |
} | |
/** | |
* @param context | |
* @return true if pattern set, false if not (or if an issue when checking) | |
*/ | |
private static boolean isPatternSet(Context context) | |
{ | |
ContentResolver cr = context.getContentResolver(); | |
try | |
{ | |
int lockPatternEnable = Settings.Secure.getInt(cr, Settings.Secure.LOCK_PATTERN_ENABLED); | |
return lockPatternEnable == 1; | |
} | |
catch (Settings.SettingNotFoundException e) | |
{ | |
XLog.e(e.getMessage()); | |
return false; | |
} | |
} | |
/** | |
* @param context | |
* @return true if pass or pin set | |
*/ | |
private static boolean isPassOrPinSet(Context context) | |
{ | |
KeyguardManager keyguardManager = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE); //api 16+ | |
return keyguardManager.isKeyguardSecure(); | |
} |
return keyguardManager.isKeyguardSecure(); return true even if SIM card is locked.
See: https://developer.android.com/reference/kotlin/android/app/KeyguardManager.html#iskeyguardsecure
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
LOCK_PATTERN_ENABLED is deprecated from API 23. It throws a SecurityException. is there a workaround to check for a pattern or android doens't consider it as secure enough for device runing API 23 and above ?
update : I'm doing it this way : https://stackoverflow.com/a/46464312/1281350