【问题标题】:How to set a fallback method if fingerprint does not work如果指纹不起作用,如何设置回退方法
【发布时间】:2019-07-08 05:16:13
【问题描述】:

我最近将我的项目移至 AndroidX,在为应用程序实现指纹时,我正在使用 AndroidX 的 Biometric。

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

当显示使用指纹进行身份验证的对话框时,该对话框将“取消”选项设置为否定按钮。

 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.

因此,我可以说“使用密码”代替“取消”按钮,以提供另一种方法以防指纹失败,当用户单击它时,我可以显示另一个弹出对话框,让用户输入设备密码以帮助检索密钥库中的应用程序密码。它是否正确 ?

但是,如果我没有设置密码来解锁我的手机,而是使用了图案,会发生什么?

我看到如果我使用 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 不使用这种回退方法?

【问题讨论】:

  • 我使用的是 alpha04,但 setDeviceCredentialAllowed() 不可用。我认为使用 AndroidX 组件的整个想法是避免使用 SDK 级别的代码......也许 Google 计划在未来添加它?

标签: android fingerprint androidx android-fingerprint-api android-biometric-prompt


【解决方案1】:

最近在 beta01 中添加了 setDeviceCredentialAllowed API

在此处查看发行说明

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

【讨论】:

  • 不再可用。新的解决方案是什么?
【解决方案2】:

在 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)
}

【讨论】:

  • 你能发布onactivity结果代码吗?
【解决方案3】:

在 BiometricPromopt 上尝试 setDeviceCredentialAllowed(true)。

【讨论】:

    【解决方案4】:

    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

    【讨论】:

    • 警告:不能为同一个 BiometricPrompt 实例调用 setDeviceCredentialAllowed() 和 setNegativeButtonText()。
    • @solidogen 谢谢,我猜图书馆自 1.0.0 以来已经改变,会更新我的答案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-15
    • 1970-01-01
    • 2013-05-02
    • 1970-01-01
    • 2017-04-09
    • 2018-03-30
    • 2019-10-01
    相关资源
    最近更新 更多