首页 > 解决方案 > 为什么 Xamarin Android 中的 BiometricPrompt API 不包含 onAuthenticationError 回调

问题描述

在 Android Studios 中,BiometricPrompt API 包含一个名为 onAuthenticationError 的回调,当用户在 BiometricPrompt 对话框之外触摸以及用户尝试向设备输入无效的 Biometric 时,就会触发该回调。但 Xamarin.Android 平台中可用的 BiometricPrompt API 不提供 onAuthenticationError 回调。

我使用 Android Studios 创建了一个 android 应用程序来检查 BiometricPrompt API,在那里我可以访问名为 onAuthenticationError 的回调。然后我将应用程序部署到设备并调试应用程序。当我触摸 BiometricPrompt 对话框之外的任何区域时,就会触发上述回调,并且每次我向 BiometricPrompt 提供超过 5 次无效输入时也会触发它。然后我尝试在 Xamain.Android 中开发相同的应用程序,但在那里我找不到任何名为 onAuthenticationError 的回调。当我在设备中部署和测试应用程序时,因为上述回调不可用,我不能

我的本机 android 代码片段。

@RequiresApi(api = Build.VERSION_CODES.P)
private BiometricPrompt.AuthenticationCallback getAuthenticationCallback() {

return new BiometricPrompt.AuthenticationCallback() {
    @Override
  **  public void onAuthenticationError(int errorCode,
                                      CharSequence errString) {
        notifyUser("Authentication error: " + errString);
        super.onAuthenticationError(errorCode, errString);
    }**

    @Override
    public void onAuthenticationHelp(int helpCode,
                                     CharSequence helpString) {
        super.onAuthenticationHelp(helpCode, helpString);
    }

    @Override
    public void onAuthenticationFailed() {
        super.onAuthenticationFailed();
    }

    @Override
    public void onAuthenticationSucceeded(
            BiometricPrompt.AuthenticationResult result) {
        notifyUser("Authentication Succeeded");
        super.onAuthenticationSucceeded(result);
    }
};

}

我的 Xamarin.Android 代码片段

public class BiometricAuthCallBacks : BiometricPrompt.AuthenticationCallback
{
public TaskCompletionSource<LocalAuthStatus> promise = new TaskCompletionSource<LocalAuthStatus>();

private int failCount;

public override void OnAuthenticationSucceeded(BiometricPrompt.AuthenticationResult result)
{
    base.OnAuthenticationSucceeded(result);
    promise.TrySetResult(LocalAuthStatus.Success);
    //Success(result);
}

public override void OnAuthenticationFailed()
{
    base.OnAuthenticationFailed();
    failCount++;
    if (failCount>=5)
    {
        promise.TrySetResult(LocalAuthStatus.Fail);
    }
    //Failed();
}

public override void OnAuthenticationHelp([GeneratedEnum] BiometricAcquiredStatus helpCode, ICharSequence helpString)
{
    base.OnAuthenticationHelp(helpCode, helpString);
    promise.TrySetResult(LocalAuthStatus.Error);
    //Help(helpCode, helpString);
}

}

我的问题是为什么我不能从 Xamarin.Android 平台访问 onAuthenticationError 回调,我该如何解决这个问题?

标签: xamarin.android

解决方案


推荐阅读