我使用 androidx.biometric:biometric:1.0.0-beta02
中的 BiometricPrompt 进行身份验证过程。
但我面临着非常奇怪的问题。
我第一次调用biometricsPromt.authenticate()
一切正常,库根据设置的安全方法(模式、指纹、密码等)向我显示屏幕,然后调用回调一切都很好。
问题是当我第二次调用 biometricsPromt.authenticate()
时出现错误:
E/DeviceCredentialHandler: onCreate: Executor and/or callback was null!
我当然传递了执行器和回调 -> 它们不为空
有人遇到同样的问题或知道问题出在哪里吗?
这就是我创建 BiometricPromt 的方式
`
companion object {
private val executor: Executor by lazy {
Executors.newSingleThreadExecutor()
}
fun showBiometricsPrompt(
activity: FragmentActivity,
callback: BiometricPrompt.AuthenticationCallback
): BiometricPrompt {
val promptInfo = BiometricPrompt.PromptInfo.Builder()
.setTitle(activity.getString(R.string.title_authentication))
.setDescription(activity.getString(R.string.msg_authenticate_first))
.setDeviceCredentialAllowed(true)
.build()
val biometricPrompt =
BiometricPrompt(
activity,
executor, callback
)
biometricPrompt.authenticate(promptInfo)
return biometricPrompt
}
`
这是我的调用方式:
`
BiometricsHelper.showBiometricsPrompt(
this,
object : BiometricPrompt.AuthenticationCallback() {
override fun onAuthenticationError(errorCode: Int, errString: CharSequence) {
super.onAuthenticationError(errorCode, errString)
showToastMsg(errString.toString())
}
override fun onAuthenticationFailed() {
super.onAuthenticationFailed()
}
override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult) {
super.onAuthenticationSucceeded(result)
}
})
`
最佳答案
这是解决此问题的方法,但不是完整的解决方案。
完成身份验证后要做的任何事情后,您需要调用 recreate()
。
这将重新启动 Activity 并允许再次显示生物识别提示。
关于android - BiometricPrompt 执行器和/或回调为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58286606/