1

我在我的 Android 应用程序中使用新的 BiometricPrompt 从以下文学

implementation 'androidx.biometric:biometric:1.0.0-rc01'

加载活动时,我能够成功显示 BiometicPrompt。

我的问题是,如果我将 Prompt 保持理想状态 30-60 秒,或者如果我将应用程序更改为后台,或者当应用程序再次出现时 Prompt 正在显示时我锁定和解锁屏幕 BiometricPrompt 被解雇/不显示。我无法确定问题是否与我的执行人有关,请帮忙。下面是我的代码

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    Executor executor = Executors.newFixedThreadPool(2);

    final BiometricPrompt biometricPrompt = new BiometricPrompt(this,
            executor, new BiometricPrompt.AuthenticationCallback() {
        @Override
        public void onAuthenticationError(int errorCode, @NonNull CharSequence errString) {
            super.onAuthenticationError(errorCode, errString);
        }

        @Override
        public void onAuthenticationSucceeded(@NonNull BiometricPrompt.AuthenticationResult result) {
            super.onAuthenticationSucceeded(result);
            navigateHome();
        }

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

    final BiometricPrompt.PromptInfo promptInfo = new BiometricPrompt.PromptInfo.Builder()
            .setTitle("Authenticate")
            .setSubtitle("Unlock with your fingerprint")
            .setNegativeButtonText("Cancel")
            .build();

    biometricPrompt.authenticate(promptInfo);

}

2019 年 2 月 13 日更新

我更新到

implementation 'androidx.biometric:biometric:1.0.1'

并更改执行者如下

Executor executor = ContextCompat.getMainExecutor(this);

现在大部分的问题都解决了,除了一加设备带屏下指纹扫描仪(6T及以上机型)。在提供不正确的手指扫描(第一次本身)时,提示正在调用onAuthenticationFailed()而不是onAuthenticationError(..)

4

3 回答 3

2

如答案中所述,生物识别提示有一种故意行为,即不会在后台重新提示一次。

但是有一个解决方法,在您的 Activity 中使用 WindowFocusChanged

@Override
    public void onWindowFocusChanged(boolean hasFocus){
        if(hasFocus){
            biometricPromptFunction();
        }
    }

基本上,当应用程序从前台返回时,焦点在当前活动上,并启动生物识别提示。请记住,即使它被取消或取消,它也会继续提示。为避免这种情况,您需要保留一个计数器,该计数器在 windowFocus 更改并在 onResume 函数上重置时递增。

于 2020-02-13T06:25:53.853 回答
2

那是根据设计。应用程序不应该无限期地等待生物特征认证。不同设备之间的超时时间可能会有所不同。

当超时发生时,我希望您能以错误代码的形式调用onAuthenticationErrorwith 。BiometricConstants.BIOMETRIC_ERROR_TIMEOUT

我不知道为什么 Google 选择不通过BiometricManager. 但是,任何调用onAuthenticationError都应被视为不可恢复的错误,并且身份验证已结束。

于 2019-10-23T08:59:26.723 回答
0

当任务堆栈切换(用户转到不同的应用程序或主屏幕),或者当设备进入键盘保护/屏幕关闭时,BiometricPrompt 消失。未恢复身份验证是有意的行为,因为用户有可能恢复应用程序并且已经忘记了上下文。

于 2019-11-05T01:35:20.443 回答