微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Android 生物识别 API 问题

如何解决Android 生物识别 API 问题

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

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

我做了什么:

依赖:

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

任何帮助将不胜感激。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。