簡體   English   中英

如何從生物識別提示的確認屏幕收聽取消事件?

[英]How to listen the cancel event from confirmation screen of a biometric prompt?

我正在我的應用程序中實施最新的生物識別提示,並使確認要求為真。 在進行面部識別時,它會提示用戶確認成功的面部驗證。 如果用戶在確認提示中單擊取消意味着我需要執行我的 function,但我沒有看到該取消事件的任何回調覆蓋以執行我的 function。請幫我解決這個問題。

該取消事件不會在 AuthenticationCallBack 中偵聽。 而且,我在 android 文檔或論壇中沒有看到任何關於此問題的建議或解決方案。

fun initBiometricPrompt(
    activity: AppCompatActivity,
    listener: BiometricAuthListener
): BiometricPrompt {
    val callback = object : BiometricPrompt.AuthenticationCallback() {
        override fun onAuthenticationError(errorCode: Int, errString: CharSequence) {
            super.onAuthenticationError(errorCode, errString)
            listener.onBiometricAuthenticationError(errorCode, errString.toString())
        }

        override fun onAuthenticationFailed() {
            super.onAuthenticationFailed()
            Timber.tag("BioMetricAuthentication").e("Authentication failed for an unknown reason")
        }

        override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult) {
            super.onAuthenticationSucceeded(result)
            listener.onBiometricAuthenticationSuccess(result)
        }
    }
    return BiometricPrompt(activity, getMainExecutor(activity), callback)
}

fun showBiometricPrompt(
    @StringRes
    title: Int = R.string.unlock_app
    showSubtitle: Boolean = true,
    activity: AppCompatActivity,
    listener: BiometricAuthListener,
    allowDeviceCredential: Boolean = true
) {
    val promptInfo = BiometricPrompt.PromptInfo.Builder()
        .setTitle(activity.getString(title))
        .setConfirmationRequired(true)
        .setSubtitle((activity.getString(R.string.confirm_your_screen_lock)))
        .apply {
            if (allowDeviceCredential) {
                setDeviceCredentialAllowed(allowDeviceCredential)
            } else {
                setNegativeButtonText(activity.getString(R.string.cancel))
            }
        }.build()
    val biometricPrompt = initBiometricPrompt(activity, listener)
    biometricPrompt.authenticate(promptInfo)
}

我認為您想要做的是檢查錯誤代碼並根據其值采取行動。 在您的特定情況下,您想要捕獲錯誤代碼BiometricPrompt.ERROR_USER_CANCELEDBiometricPrompt.ERROR_NEGATIVE_BUTTON

示例代碼塊:

override fun onAuthenticationError(errorCode: Int, errString: CharSequence) {
        super.onAuthenticationError(errorCode, errString) //keep in mind, that this call closes the authentication session
        if (errorCode == BiometricPrompt.ERROR_CANCELED
            || errorCode == BiometricPrompt.ERROR_LOCKOUT
            || errorCode == BiometricPrompt.ERROR_LOCKOUT_PERMANENT
        ) {
            // show a toast and navigate back
        } else if (errorCode == BiometricPrompt.ERROR_USER_CANCELED
            || errorCode == BiometricPrompt.ERROR_NEGATIVE_BUTTON
        ) {
            // do something else
        }
        ...
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM