【问题标题】:How to make BiometricPrompt non-cancelable?如何使 BiometricPrompt 不可取消?
【发布时间】:2019-06-06 10:16:44
【问题描述】:

我在我的应用程序中使用BiometricPrompt。它运行良好,并在调用 authenticate() 方法时显示对话框。但是当我在对话框外部单击时,此对话框会关闭。如何预防?如何使 BiometricPrompt 的对话框不可取消?这里没有像biometricPrompt.setCancelable(false) 这样的方法。

【问题讨论】:

  • 似乎该功能不可用,我建议使用自定义对话框用于相同目的。
  • 你试过setCanceledOnTouchOutside(false)吗?
  • @MadLeo 来自文档:“在 Android 9 及更高版本中,不推荐使用 FingerprintManager API。如果您的捆绑应用和系统应用使用此 API,请将它们更新为使用 BiometricPrompt”。 source.android.com/security/biometric
  • @MehulSolanki 如我所见,没有可用此名称的方法:developer.android.google.cn/reference/kotlin/androidx/biometric/…
  • 你可以在 onAuthenticationError 中使用错误代码 BiometricPrompt.ERROR_USER_CANCELED 处理它

标签: android androidx android-biometric-prompt


【解决方案1】:

BiometricPrompt 不允许这样做。因此,您将无法将系统提供的生物识别提示设为不可取消。但是您可以检测到用户何时取消对话框。

所以一个选项是,在用户取消后再次显示生物识别提示(我认为这将是一个糟糕的用户体验)或使用备用用户身份验证:

override fun onAuthenticationError(errorCode: Int, errString: CharSequence) {
        if (errorCode == BiometricConstants.ERROR_USER_CANCELED) {
            // User canceled the operation

            // you can either show the dialog again here

            // or use alternate authentication (e.g. a password) - recommended way
        }
    }

【讨论】:

【解决方案2】:

检查一下

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P) {
    supportFragmentManager.fragments.forEach {
        if(it is DialogFragment) {
            it.dialog?.setCanceledOnTouchOutside(false)
        }
    }
}

【讨论】:

  • 生物识别提示不一定是对话框,具体取决于设备。例如,在 OnePlus 设备上,可能会显示全屏视图。
【解决方案3】:

有些设备仍然存在此问题。一种解决方法是获取根视图并添加一个覆盖视图,并将可点击方法设置为 false。

    ViewGroup  viewGroup =  ((ViewGroup) yourActivity.findViewById(android.R.id.content)).getChildAt(0);

    //create your view
    Display display = mActivity.getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    View view = new View(yourActivity);
    view.setId(R.id.overlay_view);
    view.setLayoutParams(new ViewGroup.LayoutParams(size.x, size.y));
    view.setBackgroundColor(ContextCompat.getColor(yourActivity, R.color.black));
    view.setOnClickListener(v -> {
        //do nothing prevent click under this overlay
    });

    //add your view on top of the screen
    viewGroup.addView(view);

    //call your biometric dialog
    ....

    //on callbacks even if it is error or success call remove view
    viewGroup.removeView(view);

【讨论】:

    【解决方案4】:

    您必须使用版本 1.0.0-beta01 或更高版本。

    现在是默认行为:
    触摸外部不再取消身份验证。后退按钮仍然取消身份验证。

    你可以看到changelog:

    将行为更改为不允许 BiometricPrompt 被提示之外的触摸事件取消。

    您也可以查看rewiew report
    没有新的 API。

    【讨论】:

    • FragmentManager 更新到 1.0,1 后已经在执行事务错误
    • 嗨 Gabriele,你有没有机会知道如何实现 onDismiss 或 onCancel 监听器之类的东西?我想知道这是否可能。
    • 我尝试过使用 v1.0.1 和 v1.1.0-alpha01。当我在外面点击时,对话框被关闭。任何帮助将不胜感激。
    • 投反对票,因为在 1.1.0-rc01 的提示之外单击时提示被关闭
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-22
    • 2019-08-16
    相关资源
    最近更新 更多