问题描述
我的应用程序包含用于登录的用户身份验证(包括密码/图案、指纹解锁),这取决于设备安全性。我正在使用生物识别管理器使用 BiometricManager 检测设备是否具有指纹支持,并使用 isDeviceSecure() 检查设备是否受到保护。我需要检测移动设备在哪种模式下是安全的,无论是带针/图案、带指纹的针/图案、带面部解锁的针/图案还是所有三种模式(针/图案、面部解锁、指纹)。
解决方法
这里是检测设置的锁类型的代码
将库添加到 build.gradle
implementation 'androidx.biometric:biometric:1.0.0-beta01'
并将此代码添加到您的活动
KeyguardManager keyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
boolean keyguardSecure = keyguardManager.isKeyguardSecure();
Log.e("---","checkSecurityTypes: keyguardLocked - " + keyguardSecure);//true = pin/pattern
int i = BiometricManager.from(this).canAuthenticate();
Log.e("---","checkSecurityTypes: " + i);//true 0 = pin/pattern with finger print
switch (i) {
case BiometricManager.BIOMETRIC_SUCCESS:
Log.d("MY_APP_TAG","App can authenticate using biometrics.");
break;
case BiometricManager.BIOMETRIC_ERROR_NO_HARDWARE:
Log.e("MY_APP_TAG","No biometric features available on this device.");
break;
case BiometricManager.BIOMETRIC_ERROR_HW_UNAVAILABLE:
Log.e("MY_APP_TAG","Biometric features are currently unavailable.");
break;
case BiometricManager.BIOMETRIC_ERROR_NONE_ENROLLED:
// Prompts the user to create credentials that your app accepts.
break;
}
if (i == 0 && keyguardSecure) {
//fingerprint is always with pin/pattern/password
Log.e("---","checkSecurityTypes: fingerprint is set with pin/pattern");
} else if (keyguardSecure) {
//true if pin/pattern/password is set
Log.e("---","checkSecurityTypes: pin/pattern is set");
}
我们无法检测人脸类型。有关更多信息,请参阅此 link