关于Android上BiometricPrompt的一些问题

我正在尝试找出如何(如果可能)修改biometricPrompt的正常行为,尤其是当认证失败时,我想显示Gandalf。 我当前正在使用自定义的alertDialog显示它,但它仍在后台,而biometricPrompt片段位于前景exactly like this上,它失去了所有的笨拙... 最好的解决方案可能是在前景上同时显示alertDialog和biometricPrompt,仅在屏幕的上半部分显示图像,但是目前我不知道该怎么做,或者更好的是,我没有想出如何将布局链接在一起以管理尺寸/边距以及其他所有内容。

我在想的另一件事是删除biometricPrompt,因此警报对话框将放在前台,但是我尝试过的任何解决方案都失败了。

任何类型的帮助/想法都将受到欢迎。

无论如何,这是代码:

class BiometricPromptManager(private val activity: Fragmentactivity) {

private val cryptoManager = CryptoManager(activity)

fun authenticateAndDecrypt(failedaction: () -> Unit,successaction: (String) -> Unit) {

    //  display biometric prompt,if the user is authenticated,the decryption will start
    // if biometric related decryption gives positives results,the successaction will start services data decryption

    val executor = Executors.newSingleThreadExecutor()
    val biometricPrompt = BiometricPrompt(activity,executor,object : BiometricPrompt.AuthenticationCallback() {

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

            cryptoManager.startDecrypt(failedaction,successaction)
        }

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

            activity.runOnUiThread { failedaction() }
        }

        override fun onAuthenticationFailed() {
            super.onAuthenticationFailed()

            activity.runOnUiThread { failedaction() }
        }
    })

    val promptInfo = biometricPromptInfo()
    biometricPrompt.authenticate(promptInfo)
}

    private fun biometricPromptInfo(): BiometricPrompt.PromptInfo {
        return BiometricPrompt.PromptInfo.Builder()
            .setTitle("Fingerprint Authenticator")
            .setNegativeButtonText(activity.getString(android.R.string.cancel))
            .build()
    }
}

从活动中打开生物特征认证:

private fun openBiometricAuth(){

    if(sharedPreferences.getBoolean("fingerPrintEnabled",false)) {
        if (BiometricManager.from(this).canAuthenticate() == BiometricManager.BIOMETRIC_SUCCESS) { // check for hardware/permission
            biometric.visibility = View.VISIBLE
            BiometricPromptManager(this).authenticateAndDecrypt(::failure,::callDecryption)
        }
    }
}

无法识别用户时该怎么办:

private fun failure(){

    val view = layoutInflater.inflate(R.layout.gandalf,null)

    val builder = AlertDialog.Builder(this)

    builder.setView(view)

    builder.setPositiveButton("Dismiss") { dialog: DialogInterface,id: Int -> dialog.cancel() }

    val alertDialog = builder.create()

    alertDialog.show()
}
yangshegnhui 回答:关于Android上BiometricPrompt的一些问题

Biometric API本身通过以下方式处理身份验证失败的尝试:

  1. 对于每次失败的尝试,都会调用onAuthenticationFailed()回调。
  2. 用户获得5次尝试,第5次尝试失败后,onAuthenticationError()回调收到错误代码ERROR_LOCKOUT,用户必须等待30秒才能再次尝试。

onAuthenticationFailed()中显示对话框可能太急了,可能会导致不良的用户体验。因此,获得ERROR_LOCKOUT之后可能是个好地方。 AndroidX生物特征库的工作方式是在发送错误时将其关闭。因此,此时显示自己的对话框应该没有什么问题。

在任何情况下(即,超出这些意见范围),更通用的方法是调用cancelAuthentication()消除提示,然后继续以这种方式显示自己的对话框。

也请遵循blogpost1blogpost2来了解BiometricPrompt的推荐设计模式。

本文链接:https://www.f2er.com/3058874.html

大家都在问