我正在尝试找出如何(如果可能)修改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()
}