How to detect if PIN/password/pattern is required to unlock phone?

核能气质少年 提交于 2019-12-18 11:28:07

问题


How can I detect if the phone is locked by a password, pin or pattern?

thank you!


回答1:


Two methods

  1. Check programatically - API 16+

https://gist.github.com/doridori/54c32c66ef4f4e34300f

Note that you dont need to check for face unlock as that requires that a pin/pass fallback is set.

  1. Device Admin Policies

Can also look into the Device Admin Policies which allow restrictions on how the app is setup regarding security including pin/pass set restrictions

  • Device Administration
  • Enhancing Security with Device Management Policies

As an aside, these are the lock types you want to check for if using an encrypted Keystore. Check here for more info.




回答2:


You can use the Settings.Secure class to query information about the security in place on an android device. For example, to see if the user has a lock pattern enabled you'd do:

ContentResolver cr = getContentResolver();
int lockPatternEnable = 
  Settings.Secure.getInt(cr, Settings.Secure.LOCK_PATTERN_ENABLED);

lockPatternEnable would then have a 0 if it wasn't enabled and a 1 if it was enabled.




回答3:


This should be OK for Android API 16 and up, according to the documentation. I tested it on 19 and it seems to work.

private boolean IsDeviceSecured () {
    KeyguardManager keyguardManager =
            (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE); //api 16+
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        return keyguardManager.isDeviceSecure();
    }
    return keyguardManager.isKeyguardSecure ();
}


来源:https://stackoverflow.com/questions/7879143/how-to-detect-if-pin-password-pattern-is-required-to-unlock-phone

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