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

心不动则不痛 提交于 2019-12-09 13:03:09

问题


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?


回答1:


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);



回答2:


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
}



回答3:


You can check with KeyguardManager's method isKeyguardSecure() to make sure if lock or passcode is enabled or not.

KeyguardManager keyguardManager = (KeyguardManager) getSystemService(KEYGUARD_SERVICE); Toast.makeText(this,"KeyGuardEnabled ? "+ keyguardManager.isKeyguardSecure(),Toast.LENGTH_LONG).show();



来源:https://stackoverflow.com/questions/21748979/android-4-3-how-can-i-check-if-user-has-lock-enabled

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!