【问题标题】:BiometricPrompt in android is not supporting exit on back buttonandroid中的BiometricPrompt不支持退出按钮
【发布时间】:2020-07-15 15:36:06
【问题描述】:

我正在使用 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);
    }
}

【问题讨论】:

    标签: android android-biometric-prompt


    【解决方案1】:

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

    更新:基于您的编辑

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

    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()
                }
            }
    

    【讨论】:

    • 我已经推荐了您的博客,发现它很有用,但我的问题不同,我将指纹活动与其他活动联系起来。我在我的问题上添加了代码。
    • 感谢您的帮助。感谢。这是一项长期悬而未决的任务,非常感谢。
    • 如果此回复有助于解决您的问题,请投票接受,以便其他求职者知道
    • 在 Jetpack Compose 上,将 onAuthenticationError() 转换为挂起函数,我无法调用 finish()。相反,如果errorCode == ERROR_USER_CANCELED 使用continuation.resume(),我将返回null。获取null 后,托管生物识别提示的可组合组件能够正确弹出。
    【解决方案2】:

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

    biometricPrompt.cancelAuthentication()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-13
      • 1970-01-01
      • 2016-04-16
      相关资源
      最近更新 更多