How to make BiometricPrompt non-cancelable?

时光怂恿深爱的人放手 提交于 2021-01-27 03:54:07

问题


I am using BiometricPrompt in my application. It works well and shows the dialog when call the authenticate() method. But this dialog gets closing when I click outside the dialog. How to prevent it? How to make BiometricPrompt's dialog non-cancelable? Here is no method like biometricPrompt.setCancelable(false).


回答1:


You have to use the version 1.0.0-beta01 or later.

Now it is the default behavior:
Touches outside no longer cancel authentication. Back button cancel authentication still.

You can see the changelog:

Changed behavior to not allow BiometricPrompt to be cancelled by a touch event outside the prompt.

You can check also the rewiew report.
No new API.




回答2:


BiometricPrompt does not allow that. So you won't be able to make the system-provided biometric prompt non-cancelable. But you can detect whenever user cancels the dialog.

So an option would be, to show again the biometric prompt after user cancel it (which I think would be a bad user experience) or use alternate user authentication:

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



回答3:


check it out

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



回答4:


There are some devices that still have this issue. An work around will be to get root view and add an overlay view with clickable method set to 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);


来源:https://stackoverflow.com/questions/56475507/how-to-make-biometricprompt-non-cancelable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!