如何使 BiometricPrompt 不可取消?
How to make BiometricPrompt non-cancelable?
我在我的应用程序中使用 BiometricPrompt
。它运行良好,并在调用 authenticate()
方法时显示对话框。但是,当我在对话框外单击时,此对话框会关闭。如何预防?如何使 BiometricPrompt 的对话框不可取消?这里没有像 biometricPrompt.setCancelable(false)
.
这样的方法
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
}
}
看看
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P) {
supportFragmentManager.fragments.forEach {
if(it is DialogFragment) {
it.dialog?.setCanceledOnTouchOutside(false)
}
}
}
您必须使用 1.0.0-beta01 或更高版本。
现在是默认行为:
触摸外部不再取消身份验证。后退按钮仍然取消身份验证。
您可以看到 changelog:
Changed behavior to not allow BiometricPrompt
to be cancelled by a touch event outside the prompt.
您还可以查看 rewiew report。
没有新的API.
有些设备仍然存在这个问题。解决方法是获取根视图并添加可点击方法设置为 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);
我在我的应用程序中使用 BiometricPrompt
。它运行良好,并在调用 authenticate()
方法时显示对话框。但是,当我在对话框外单击时,此对话框会关闭。如何预防?如何使 BiometricPrompt 的对话框不可取消?这里没有像 biometricPrompt.setCancelable(false)
.
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
}
}
看看
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P) {
supportFragmentManager.fragments.forEach {
if(it is DialogFragment) {
it.dialog?.setCanceledOnTouchOutside(false)
}
}
}
您必须使用 1.0.0-beta01 或更高版本。
现在是默认行为:
触摸外部不再取消身份验证。后退按钮仍然取消身份验证。
您可以看到 changelog:
Changed behavior to not allow
BiometricPrompt
to be cancelled by a touch event outside the prompt.
您还可以查看 rewiew report。
没有新的API.
有些设备仍然存在这个问题。解决方法是获取根视图并添加可点击方法设置为 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);