如果指纹不起作用,如何设置回退方法

How to set a fallback method if fingerprint does not work

我最近将我的项目移至 AndroidX,在为应用程序实现指纹时,我正在使用 AndroidX 的生物识别技术。

implementation 'androidx.biometric:biometric:1.0.0-alpha03'

当显示对话框以使用指纹进行身份验证时,该对话框将 "Cancel" 选项设置为否定按钮。

 final BiometricPrompt.PromptInfo promptInfo = new BiometricPrompt.PromptInfo.Builder()
            .setTitle("Log into App")
            .setSubtitle("Please touch the fingerprint sensor to log you in")
            .setDescription("Touch Sensor")
            .setNegativeButtonText("Cancel".toUpperCase())
            .build();

根据 android 文档: https://developer.android.com/reference/androidx/biometric/BiometricPrompt.PromptInfo.Builder.html#setNegativeButtonText(java.lang.CharSequence)

Required: Set the text for the negative button. 
This would typically be used as a "Cancel" button, but may be also used
to show an alternative method for authentication, 
such as screen that asks for a backup password.

所以我可以说 "Use Password" 代替 "Cancel" 按钮来提供替代方法,以防指纹失败,当用户点击它时,我可以显示另一个弹出对话框,让用户输入设备密码,以帮助从密钥库中检索应用程序密码。这个对吗 ?

但是,如果我没有设置密码来解锁我的 phone 而是使用模式会怎样?

我看到如果我使用 android.hardware.biometrics.BiometricPrompt.Builder 而不是 androidx.biometric.BiometricPrompt.PromptInfo.Builder,它有一个方法 https://developer.android.com/reference/android/hardware/biometrics/BiometricPrompt.Builder.html#setDeviceCredentialAllowed(boolean) 出于同样的目的,如果指纹失败,让用户使用其他方式进行身份验证。

有人能帮我理解一下吗?我如何使用 AndroidX 实现这一点,因为我的应用程序从 API 16 开始兼容。为什么 AndroidX 不返回这种回退方法?

在 BiometricPromopt 上尝试 setDeviceCredentialAllowed(true)。

最近在 beta01 中添加了 setDeviceCredentialAllowed API

在此处查看发行说明

https://developer.android.com/jetpack/androidx/releases/biometric

在 SDK 版本 Q 及更高版本上使用 BiometricPrompt 和身份验证回调,否则使用 createConfirmDeviceCredentialsIntent.

val km = getSystemService(Context.KEYGUARD_SERVICE) as KeyguardManager

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
    val biometricPrompt = BiometricPrompt.Builder(this)
            .setTitle(getString(R.string.screen_lock_title))
            .setDescription(getString(R.string.screen_lock_desc))
            .setDeviceCredentialAllowed(true)
            .build()

    val cancellationSignal = CancellationSignal()
    cancellationSignal.setOnCancelListener {
        println("@Biometric cancellationSignal.setOnCancelListener")
        //handle cancellation
    }

    val executors = mainExecutor
    val authCallBack = object : BiometricPrompt.AuthenticationCallback() {
        override fun onAuthenticationError(errorCode: Int, errString: CharSequence?) {
            super.onAuthenticationError(errorCode, errString)
            print("SecuritySetupActivity.onAuthenticationError ")
            println("@Biometric errorCode = [${errorCode}], errString = [${errString}]")
            //handle authentication error
        }

        override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult?) {
            super.onAuthenticationSucceeded(result)
            print("SecuritySetupActivity.onAuthenticationSucceeded ")
            println("@Biometric result = [${result}]")

            //handle authentication success
        }

        override fun onAuthenticationHelp(helpCode: Int, helpString: CharSequence?) {
            super.onAuthenticationHelp(helpCode, helpString)
            print("SecuritySetupActivity.onAuthenticationHelp ")
            println("@Biometric helpCode = [${helpCode}], helpString = [${helpString}]")
        }

        override fun onAuthenticationFailed() {
            super.onAuthenticationFailed()
            print("SecuritySetupActivity.onAuthenticationFailed ")

            //handle authentication failed
        }
    }



    biometricPrompt.authenticate(cancellationSignal, executors, authCallBack)


} else {
    val i = km.createConfirmDeviceCredentialIntent(getString(R.string.screen_lock_title), getString(R.string.screen_lock_desc))
    startActivityForResult(i, 100)
}

androidx 1.0.0 允许您轻松设置回退 - 如下所示:

    // Allows user to authenticate using either a Class 3 biometric or
    // their lock screen credential (PIN, pattern, or password).
    promptInfo = BiometricPrompt.PromptInfo.Builder()
        .setTitle("Biometric login for my app")
        .setSubtitle("Log in using your biometric credential")
        // Can't call setNegativeButtonText() and
        // setAllowedAuthenticators(... or DEVICE_CREDENTIAL) at the same time.
        // .setNegativeButtonText("Use account password")
        .setAllowedAuthenticators(BIOMETRIC_STRONG or DEVICE_CREDENTIAL)
        .build()

this