【问题标题】:Android Biometric API IssueAndroid 生物识别 API 问题
【发布时间】:2021-04-23 16:51:53
【问题描述】:

我想要实现的目标: 提供使用指纹、FaceId 和虹膜等不同生物特征登录的选项。我想提供使用 PIN、密码或图案的选项,以防任何生物识别传感器不起作用。

问题:当用户点击“Use Password”选项时,它直接转到“onAuthenticationError”回调,我正在检查错误BiometricPrompt.ERROR_NEGATIVE_BUTTON 的代码。我担心的是,我需要自己处理吗?我的意思是我是否需要显示一个对话框弹出窗口,我会要求用户输入他/她的电子邮件/用户名和密码,然后他/她可以登录我的应用程序?

我做了什么:

依赖:

implementation 'androidx.biometric:biometric:1.2.0-alpha01'

MainActivity.kt

    class MainActivity : AppCompatActivity() {

    private lateinit var executor: Executor
    private lateinit var biometricPrompt: BiometricPrompt

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        executor = ContextCompat.getMainExecutor(this)
        biometricPrompt = createBiometricObject()
    }

    private fun createBiometricObject(): BiometricPrompt {

        return BiometricPrompt(this, executor, object : AuthenticationCallback() {

            override fun onAuthenticationError(errorCode: Int, errString: CharSequence) {
                super.onAuthenticationError(errorCode, errString)

                if (errorCode == ERROR_NEGATIVE_BUTTON && errString == "Use Password") {
                    // Do I need to create my own DialogPopUp?
                }
            }

            override fun onAuthenticationFailed() {
                super.onAuthenticationFailed()
            }

            override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult) {
                super.onAuthenticationSucceeded(result)
            }
        })
    }

    fun loginWithBiometrics(view: View) {
        when (BiometricManager.from(this).canAuthenticate(BIOMETRIC_STRONG)) {
            BiometricManager.BIOMETRIC_SUCCESS -> biometricPrompt.authenticate(
                createPromptInfoForBiometrics()
            )
            BiometricManager.BIOMETRIC_ERROR_NONE_ENROLLED -> Toast.makeText(
                this,
                "Please enroll your biometrics",
                Toast.LENGTH_LONG
            ).show()
            BiometricManager.BIOMETRIC_ERROR_NO_HARDWARE -> Toast.makeText(
                this,
                "Device not compatible",
                Toast.LENGTH_LONG
            ).show()
            BiometricManager.BIOMETRIC_ERROR_HW_UNAVAILABLE -> Toast.makeText(
                this,
                "Sensors are available as off now, please try again later!",
                Toast.LENGTH_LONG
            ).show()
            BiometricManager.BIOMETRIC_ERROR_SECURITY_UPDATE_REQUIRED -> {
                TODO()
            }
            BiometricManager.BIOMETRIC_ERROR_UNSUPPORTED -> {
                TODO()
            }
            BiometricManager.BIOMETRIC_STATUS_UNKNOWN -> {
                biometricPrompt.authenticate(createPromptInfoForBiometrics())
            }
        }
    }

    private fun createPromptInfoForBiometrics(): BiometricPrompt.PromptInfo {
        return BiometricPrompt.PromptInfo.Builder()
            .setTitle("Biometric Login")
            .setSubtitle("Please login with your biometrics")
            .setNegativeButtonText("Use Password")
            .setAllowedAuthenticators(BIOMETRIC_STRONG)
            .build()
    }
}

任何帮助将不胜感激。

【问题讨论】:

  • 生物识别具有身份验证类型,并且具有特殊配置(Class1、Class 2、Class 3 -> Default、WEAK、STRONG)-> source.android.com/compatibility/…
  • 对于我的场景,我应该使用哪一个来进行NEGATIVE BUTTON CLICK
  • 我认为这不会有什么不同,身份验证器和否定按钮之间没有关系
  • 我认为,您可以使用或者您应该了解身份验证类型如何为您的目标工作:“提供使用指纹、FaceId 和虹膜等不同生物特征登录的选项。我想提供使用 PIN 的选项,密码或图案以防任何生物识别传感器不起作用。”
  • 但是,针对具体场景。你可以试试弱吗?

标签: android android-studio android-layout biometrics android-biometric-prompt


【解决方案1】:

您应该尝试将DEVICE_CREDENTIAL 标志传递给setAllowedAuthenticators()

BiometricPrompt.PromptInfo.Builder()
                .setTitle("Biometric login for my app")
                //..
                .setAllowedAuthenticators(DEVICE_CREDENTIAL or BIOMETRIC_STRONG)
                .build()

但还要确保您使用适当的 android API 级别。根据文档:

请注意,并非所有身份验证器类型的组合都受支持 在 Android 11 (API 30) 之前。具体来说,仅 DEVICE_CREDENTIAL 是 在 API 30 之前不受支持,并且 BIOMETRIC_STRONG | DEVICE_CREDENTIAL API 28-29 不支持。设置不支持的值 受影响的 Android 版本在调用 build() 时会导致错误。

【讨论】:

    猜你喜欢
    • 2017-09-12
    • 2011-07-07
    • 1970-01-01
    • 2021-09-15
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多