1

我正在使用 android 提供的 BiometricPrompt 类在我们的应用程序中提供生物识别身份验证,它工作正常,但是当我点击手机的后退按钮时,会显示空白页面。相反,我希望应用程序在单击后退按钮时关闭。任何指针都会有所帮助。,

public class FingerprintLoginActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        BiometricPrompt biometricPrompt = new BiometricPrompt(this, Executors.newSingleThreadExecutor(), new BiometricPrompt.AuthenticationCallback() {

            @Override
            public void onAuthenticationError(int errorCode, @NonNull CharSequence errString) {
                super.onAuthenticationError(errorCode, errString);
                if (errorCode == BiometricPrompt.ERROR_NEGATIVE_BUTTON) {
                    // user clicked negative button    
                } else {
                    // TODO: Called when an unrecoverable error has been encountered and the operation is complete.
                }
            }

            @Override
            public void onAuthenticationSucceeded(@NonNull BiometricPrompt.AuthenticationResult result) {
                super.onAuthenticationSucceeded(result);
                //TODO: Called when a biometric is recognized.
                Context.Fingerprint = true;
                Intent fingerprintIntent = new Intent(FingerprintLoginActivity.this, MainActivity.class);
                fingerprintIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                startActivity(fingerprintIntent);
            }

            @Override
            public void onAuthenticationFailed() {
                super.onAuthenticationFailed();
                //TODO: Called when a biometric is valid but not recognized.
            }
        });

        BiometricPrompt.PromptInfo promptInfo = new BiometricPrompt.PromptInfo.Builder()
                .setTitle("touch to fingerprint scanner")
                .setNegativeButtonText("Cancel")
                .build();

        biometricPrompt.authenticate(promptInfo);
    }
}
4

2 回答 2

1

我根据此处此处给出的说明实施。后退按钮对我来说很好用:对话框/提示只是关闭,我回到活动。你认得你看到的空白页吗?也许你正在一个空白的活动中实现 API?尝试关注上面提到的博客文章,让我们知道它是如何进行的。

更新:根据您的编辑

由于您想在用户单击后退按钮时退出 Activity,因此您必须BiometricPrompt.ERROR_USER_CANCELED通过调用在代码中进行处理finish()

override fun onAuthenticationError(errorCode: Int, errString: CharSequence) {
            super.onAuthenticationError(errorCode, errString)
            Log.d(TAG, "onAuthenticationError -> $errorCode :: $errString")
            if (errorCode == BiometricPrompt.ERROR_NEGATIVE_BUTTON) {
                loginWithPassword() 
            }else if(errorCode == BiometricPrompt.ERROR_USER_CANCELED){
                finish()
            }
        }
于 2020-01-13T18:45:41.703 回答
1

要关闭指纹对话框,您必须调用:

biometricPrompt.cancelAuthentication()

于 2020-07-15T07:36:06.307 回答