关于Android上的生物识别提示的一些问题



我正试图弄清楚如何修改(如果可能的话(生物识别提示的正常行为,特别是当身份验证失败时,我想显示Gandalf。我现在用一个自定义的alertDialog显示它,但它仍然在后台,前台的生物识别提示片段就像这样,它失去了所有的愚蠢。。。最好的解决方案可能是在前台同时显示alertDialog和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()
}

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

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

onAuthenticationFailed()中显示对话框可能过于急切,可能会导致用户体验不佳。所以一个好地方可能是在你拿到ERROR_LOCKOUT之后。AndroidX生物识别库的工作方式是,当发送错误时,它会取消生物识别提示。因此,此时显示自己的对话框应该没有什么问题。

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

此外,请关注博客1和博客2,了解BiometricPrompt的推荐设计模式。

最新更新