Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android 4.3 : How can I check if user has lock enabled?

I want my app to behave differently (not store stuff) if the user does not have a lock screen or only swipe enabled. The top answer here: check whether lock was enabled or not has been edited to say the code no longer works after upgrade to Android 4.3.

Is there anyway to detect this on Android 4.3?

like image 337
Erlend Avatar asked Feb 13 '14 08:02

Erlend


2 Answers

Ok, so it seems it is possible with some reflection. Not the best solution, but at least it works on the devices we tried:

        Class<?> clazz = Class.forName("com.android.internal.widget.LockPatternUtils");
        Constructor<?> constructor = clazz.getConstructor(Context.class);
        constructor.setAccessible(true);
        Object utils = constructor.newInstance(context);
        Method method = clazz.getMethod("isSecure");
        return (Boolean)method.invoke(utils);
like image 161
Erlend Avatar answered Oct 13 '22 05:10

Erlend


You can check it with KeyguardManager:

KeyguardManager keyguardMgr = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
if(keyguardMgr.isKeyguardSecure()){
    // Secure keyguard, the keyguard requires a password to unlock
} else {
    // Insecure keyguard, the keyguard doesn't require a password to unlock
}
like image 5
MikePtr Avatar answered Oct 13 '22 05:10

MikePtr