出现上述问题可能是因为在验证时没有正确处理结果回调。要解决这个问题,可以使用以下代码例子,判断回调结果,当用户按下返回键关闭前一个提示时,手动调用cancel()函数取消验证:
if (BiometricPrompt.AUTHENTICATION_RESULT_TYPE_ERROR == authenticationResult.getAuthenticationType( )) {
biometricPrompt.cancelAuthentication( );
}
此外,还可以使用以下代码例子,将onAuthenticationError()函数用于取消出现上述问题的验证:
biometricPrompt = new BiometricPrompt.Builder(activity)
.setTitle(title)
.setNegativeButton(negativeButtonText, executor, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
authenticationCallback.onCancelled( );
dialog.dismiss( );
}
})
.build( );
biometricPrompt.authenticate(promptInfo, new BiometricPrompt.CryptoObject(yourCipher), executor, authenticationCallback);
private class AuthenticationCallback extends BiometricPrompt.AuthenticationCallback {
@Override
public void onAuthenticationError(int errorCode, @NonNull CharSequence errString) {
super.onAuthenticationError(errorCode, errString);
if (errorCode == BiometricPrompt.ERROR_USER_CANCELED) {
onCancelled( );
}
}
@Override
public void onAuthenticationFailed() {
super.onAuthenticationFailed( );
}
@Override
public void onAuthenticationSucceeded(@NonNull BiometricPrompt.AuthenticationResult result) {
super.onAuthenticationSucceeded(result);
onSuccess( );
}
public void onSuccess( ) {
//do something after authentication success
}
public void onCancelled( ) {
//do something after authentication cancelled
}
}
上一篇:Android11本地类不兼容:流类描述符serialVersionUID
下一篇:Android11bug:应用程序崩溃并显示java.lang.SecurityException错误:uidxxxxx无法显式添加类型为package.name的帐户。