如果您使用的是 compileSdkVersion 29 和 buildToolsVersion "29.0.1"。您可以使用本机检查方法。
我为 Kotlin 编写了这个函数:
fun checkForBiometrics() : Boolean {
Log.d(TAG, "checkForBiometrics started")
var canAuthenticate = true
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (Build.VERSION.SDK_INT < 29) {
val keyguardManager : KeyguardManager = applicationContext.getSystemService(KEYGUARD_SERVICE) as KeyguardManager
val packageManager : PackageManager = applicationContext.packageManager
if(!packageManager.hasSystemFeature(PackageManager.FEATURE_FINGERPRINT)) {
Log.w(TAG, "checkForBiometrics, Fingerprint Sensor not supported")
canAuthenticate = false
}
if (!keyguardManager.isKeyguardSecure) {
Log.w(TAG, "checkForBiometrics, Lock screen security not enabled in Settings")
canAuthenticate = false
}
} else {
val biometricManager : BiometricManager = this.getSystemService(BiometricManager::class.java)
if(biometricManager.canAuthenticate() != BiometricManager.BIOMETRIC_SUCCESS){
Log.w(TAG, "checkForBiometrics, biometrics not supported")
canAuthenticate = false
}
}
}else{
canAuthenticate = false
}
Log.d(TAG, "checkForBiometrics ended, canAuthenticate=$canAuthenticate ")
return canAuthenticate
}
另外,您必须在您的应用程序 gradle 文件上实现依赖:
implementation 'androidx.biometric:biometric:1.0.0-alpha04'
并使用最新的构建工具:
compileSdkVersion 29
buildToolsVersion "29.0.1"