Security 确定Android手机登录时使用的Pin、密码或模式
我试图确定登录android智能手机时是否需要密码、pin码或模式。我可以确定手机在登录时是否使用了模式,也可以确定登录时是否使用了密码/pin码,但我如何判断手机在登录时仅使用密码和pin码 以下代码是我目前掌握的代码:Security 确定Android手机登录时使用的Pin、密码或模式,security,keyguard,Security,Keyguard,我试图确定登录android智能手机时是否需要密码、pin码或模式。我可以确定手机在登录时是否使用了模式,也可以确定登录时是否使用了密码/pin码,但我如何判断手机在登录时仅使用密码和pin码 以下代码是我目前掌握的代码: //determine if phone uses pattern, pin or password at log on //lockPatternEnable returns 1 if pattern lock enabled and 0
//determine if phone uses pattern, pin or password at log on
//lockPatternEnable returns 1 if pattern lock enabled and 0 if pin/password password enabled
ContentResolver cr = getBaseContext().getContentResolver();
lockPatternEnable = Settings.Secure.getInt(cr, Settings.Secure.LOCK_PATTERN_ENABLED, 0);
//returns 1 if pin/password used. 0 if not
KeyguardManager keyguardManager = (KeyguardManager) getBaseContext().getSystemService(Context.KEYGUARD_SERVICE);
if( keyguardManager.isKeyguardSecure())
{
//it is pin or password protected
pinPasswordEnable=1;
}
else
{
//it is not pin or password protected
pinPasswordEnable=0;
}//http://stackoverflow.com/questions/6588969/device-password-in-android-is-existing-or-not/18716253#18716253
if(pinPasswordEnable==0 && lockPatternEnable==1)
{
//pattern
}
else if(pinPasswordEnable==1 && lockPatternEnable==0)
{
//pin or password
}
将此类用作,只需调用isDeviceSecured()方法 请参阅这封信
public class SecurityUtils {
public static boolean isDeviceSecured(Context context){
KeyguardManager keyguardManager = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
return keyguardManager.isDeviceSecure();
}else{
return (isPatternEnabled(context) || isPassOrPinEnabled(keyguardManager));
}
}
private static boolean isPatternEnabled(Context context){
return (Settings.System.getInt(context.getContentResolver(), Settings.System.LOCK_PATTERN_ENABLED, 0) == 1);
}
private static boolean isPassOrPinEnabled(KeyguardManager keyguardManager){
return keyguardManager.isKeyguardSecure();
}
}