Lar*_*ars 6 android locking unlock android-security
我需要检查锁屏是否有Pin或更安全的东西(密码,指纹等).我能够检查是否有Pin,密码或模式.
KeyguardManager keyguardManager = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
return keyguardManager.isKeyguardSecure();
Run Code Online (Sandbox Code Playgroud)
我的问题是,我无法检测锁屏是否是一个模式或更低的东西.我试过这个:
int lockPatternEnable = Settings.Secure.getInt(cr, Settings.Secure.LOCK_PATTERN_ENABLED);
Run Code Online (Sandbox Code Playgroud)
但它已弃用,并引发了一个错误.我也试过这个:
long mode2 = Settings.Secure.getLong(contentResolver, "lockscreen.password_type");
Run Code Online (Sandbox Code Playgroud)
但这也以SecurityException结束.
有没有办法检测锁屏是否有针(或更高)或锁定模式或更低?KeyguardManager以这种方式对我没用:/
任何帮助表示赞赏!谢谢!
/编辑
第一个错误是:
Caused by: java.lang.SecurityException: Settings.Secure.lock_pattern_autolock is deprecated and no longer accessible. See API documentation for potential replacements.
Run Code Online (Sandbox Code Playgroud)
第二个的例外是:W/System.err:android.provider.Settings $ SettingNotFoundException:lockscreen.password_type
当您使用Marshmallow或更高版本的设备时,会出现错误(https://developer.android.com/reference/android/provider/Settings.Secure.html)
我这样做:(灵感来自https://gist.github.com/doridori/54c32c66ef4f4e34300f)
public boolean isDeviceScreenLocked() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
return isDeviceLocked();
} else {
return isPatternSet() || isPassOrPinSet();
}
}
/**
* @return true if pattern set, false if not (or if an issue when checking)
*/
private boolean isPatternSet() {
ContentResolver cr = context.getContentResolver();
try {
int lockPatternEnable = Settings.Secure.getInt(cr, Settings.Secure.LOCK_PATTERN_ENABLED);
return lockPatternEnable == 1;
} catch (Settings.SettingNotFoundException e) {
return false;
}
}
/**
* @return true if pass or pin set
*/
private boolean isPassOrPinSet() {
KeyguardManager keyguardManager = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE); //api 16+
return keyguardManager.isKeyguardSecure();
}
/**
* @return true if pass or pin or pattern locks screen
*/
@TargetApi(23)
private boolean isDeviceLocked() {
KeyguardManager keyguardManager = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE); //api 23+
return keyguardManager.isDeviceSecure();
}
Run Code Online (Sandbox Code Playgroud)