【发布时间】:2019-09-19 21:25:59
【问题描述】:
如果您在活动中创建 biometricPrompt 和 promptInfo ,它工作正常。但我无法让它在片段中工作。
这是一个片段内部,它在 OnViewCreated 内部被调用。您在活动中执行相同的操作效果很好,一种解决方案是从活动中传递 biometricPrompt 和 PromptInfo 并将其传递到片段中。
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
tryToDisplayBiometricPrompt()
}
@TargetApi(Build.VERSION_CODES.M)
private fun tryToDisplayBiometricPrompt() {
//Create a thread pool with a single thread
biometricPrompt = BiometricPrompt(activity as FragmentActivity, Executors.newSingleThreadExecutor(), object : BiometricPrompt.AuthenticationCallback() {
override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult) {
super.onAuthenticationSucceeded(result)
authenticationSuccessful()
}
override fun onAuthenticationError(errorCode: Int, errString: CharSequence) {
super.onAuthenticationError(errorCode, errString)
if (errorCode == BiometricConstants.ERROR_NEGATIVE_BUTTON || errorCode == BiometricConstants.ERROR_USER_CANCELED || errorCode == BiometricPrompt.ERROR_CANCELED) return
authenticationlistener?.isBiometricAvailable = false
authenticationlistener?.onAuthenticationFailed()
}
})
promptInfo = BiometricPrompt.PromptInfo.Builder()
.setTitle(getString(R.string.biometric_title))
.setSubtitle(getString(R.string.biometric_subtitle))
.setDescription(getString(R.string.biometric_description))
.setNegativeButtonText(getString(R.string.cancel))
.build()
biometricPrompt?.authenticate(promptInfo)
}
【问题讨论】:
-
您是否尝试过使用主线程执行器而不是 Executors.newSingleThreadExecutor() 尝试将其替换为 yourContext.getMainExecutor() 以在 UI 线程中接收回调事件
-
我试过了,还是不行。
-
如果我把它放在 Handler().post {} 里面,它工作得很好。
-
所以看起来它与执行代码的线程有关.. 奇怪,奇怪的是它在 Activity 中工作正常,我仍然怀疑它与您作为参数传递的 Executor 相关跨度>
-
问题归档:issuetracker.google.com/issues/131980596 请加注星标以引起关注
标签: android biometrics android-biometric-prompt