看起来 BiometricPrompt 1.0.0 有一个错误,它保持不可见状态并抛出该异常
我在答案中建议了一个解决方法
编辑(感谢@Isai Damier):
重现方式:
最佳答案
此修复是在生物识别提示版本为 1.0.0 时引入的。此问题在 1.0.1 中重现
//------------------------------------------------ -------------------------
我想出了一个解决方法 - 在用户取消的提示上调用“cancelAuthentication”。
完整代码如下(在 native 应用程序中使用 - 这就是 promise 的原因):
private const val E_BIOMETRIC_ERR = "E_FINGER_PRINT_ERR"
private const val OPENED = "success"
private var bp: BiometricPrompt? = null
private var mAuthenticationPromise: Promise? = null
fun authenticate(activity: AppCompatActivity, title: String,
subTitle: String, description: String, authenticationPromise: Promise) {
mAuthenticationPromise = authenticationPromise
val executor = Executors.newSingleThreadExecutor()
bp?.cancelAuthentication()
bp = getBiometricPrompt(activity, executor)
val promptInfo = BiometricPrompt.PromptInfo.Builder()
.setTitle(title)
.setSubtitle(subTitle)
.setDescription(description)
.setDeviceCredentialAllowed(true)
.build()
bp!!.authenticate(promptInfo)
}
private fun getBiometricPrompt(activity: AppCompatActivity, executor: Executor): BiometricPrompt {
return BiometricPrompt(activity, executor, object : BiometricPrompt.AuthenticationCallback() {
override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult) {
super.onAuthenticationSucceeded(result)
mAuthenticationPromise?.resolve(OPENED)
}
override fun onAuthenticationError(errorCode: Int, errString: CharSequence) {
super.onAuthenticationError(errorCode, errString)
bp!!.cancelAuthentication()
}
override fun onAuthenticationFailed() {
super.onAuthenticationFailed()
bp!!.cancelAuthentication()
}
})
}
我希望它可以帮助任何人 - 欢呼