【问题标题】:FragmentManager is already executing transactions when Executing biometricPrompt?.authenticate(promptInfo) inside a FragmentFragmentManager 在 Fragment 内执行 biometricPrompt?.authenticate(promptInfo) 时已经在执行事务
【发布时间】: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


【解决方案1】:

请看下文或相同答案here

androidx.biometric:biometric:1.0.0-beta01 通过提供第二个构造函数解决了该问题。在此版本之前,我通过回复alpha03 解决了这个问题,但现在有一个实际的解决方案可用。

您可以找到beta01 发行说明here

我们为 BiometricPrompt 引入了第二个构造函数,它允许 它被托管在一个片段中(与现有的构造函数相反, 这需要一个 FragmentActivity)。

您可以找到新的BiometricPrompt 构造函数文档here

BiometricPrompt(Fragment fragment, Executor executor, BiometricPrompt.AuthenticationCallback callback)

要修复,请按照以下简单步骤操作:

  1. 更改您的 build.gradle 以使用生物识别版本1.0.0-beta01
  2. 使用新的构造函数。简而言之,将第一个参数更改为您的片段而不是活动。请参阅下面的代码更改:

    val biometricPrompt = BiometricPrompt(activity!!, executor, callback)
    // Change the above line to the below line
    val biometricPrompt = BiometricPrompt(this, executor, callback)
    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多