好的,所以我的应用程序将在没有指纹传感器但有面部解锁选项的设备上使用。我用我的手机(小米红米note 8 pro)测试了下面的代码,它有指纹和面部解锁选项。指纹解锁工作正常。但是,如果我从手机设置中删除我的指纹,面部解锁将无法正常工作。
private fun setUpBiometric() {
val biometricManager = BiometricManager.from(this)
when (biometricManager.canAuthenticate(BiometricManager.Authenticators.BIOMETRIC_WEAK or BiometricManager.Authenticators.DEVICE_CREDENTIAL)) {
BiometricManager.BIOMETRIC_ERROR_NO_HARDWARE -> {
activityBinding.biometricCheck.visibility = View.GONE
activityBinding.words.text = "No biometric features available on this device."
}
BiometricManager.BIOMETRIC_ERROR_HW_UNAVAILABLE -> {
activityBinding.biometricCheck.visibility = View.GONE
activityBinding.words.text = "Biometric features are currently unavailable."
}
BiometricManager.BIOMETRIC_ERROR_NONE_ENROLLED -> {
// Prompts the user to create credentials that your app accepts.
val enrollIntent = Intent(Settings.ACTION_BIOMETRIC_ENROLL).apply {
putExtra(
Settings.EXTRA_BIOMETRIC_AUTHENTICATORS_ALLOWED,
android.hardware.biometrics.BiometricManager.Authenticators.BIOMETRIC_STRONG or android.hardware.biometrics.BiometricManager.Authenticators.DEVICE_CREDENTIAL
)
}
this.startActivityForResult(enrollIntent, 1)
}
}
executor = ContextCompat.getMainExecutor(this)
biometricPrompt = BiometricPrompt(this, executor,
object : BiometricPrompt.AuthenticationCallback() {
override fun onAuthenticationError(
errorCode: Int,
errString: CharSequence
) {
super.onAuthenticationError(errorCode, errString)
Toast.makeText(
applicationContext,
"Authentication error: $errString", Toast.LENGTH_SHORT
)
.show()
}
override fun onAuthenticationSucceeded(
result: BiometricPrompt.AuthenticationResult
) {
super.onAuthenticationSucceeded(result)
Log.d("TAG", "onAuthenticationSucceeded: "+result.authenticationType)
val prefs = SharedPrefsRepository(this@LoginActivity)
CoroutineScope(Dispatchers.IO).launch {
prefs.saveUser(LoginResponse())
startActivity(Intent(this@LoginActivity, HomeActivity::class.java))
finish()
}
}
override fun onAuthenticationFailed() {
super.onAuthenticationFailed()
Toast.makeText(
applicationContext, getString(R.string.authetication_failed),
Toast.LENGTH_SHORT
)
.show()
}
})
promptInfo = BiometricPrompt.PromptInfo.Builder()
.setTitle(getString(R.string.biometric_login))
.setSubtitle(getString(R.string.use_biometric_for_login))
.setNegativeButtonText(getString(R.string.cancel))
.build()
}
这在点击侦听器上调用。
biometricPrompt.authenticate(promptInfo)
有人可以告诉我面部解锁该怎么做吗?