当用户被要求授予权限时,是否可以提供自定义文本以替换系统对话框?
当用户被要求授予权限时,是否可以提供自定义文本以替换系统对话框?
我们无法自定义请求权限对话框,但我们可以为用户提供一个自定义的解释,说明我们为什么需要请求以下是使用自定义解释的方法
private void checkForCameraPermission() {
// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA)) {
AlertDialog.Builder alertBuilder = new AlertDialog.Builder(this);
alertBuilder.setCancelable(true);
alertBuilder.setTitle("Camera permission necessary");
alertBuilder.setMessage("FITsociety need camera permission to read barcode.");
alertBuilder.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ActivityCompat.requestPermissions(BarCodeScannerActivity.this,
new String[]{Manifest.permission.CAMERA},
MY_PERMISSIONS_REQUEST_CAMERA);
}
});
AlertDialog alert = alertBuilder.create();
alert.show();
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.CAMERA},
MY_PERMISSIONS_REQUEST_CAMERA);
// MY_PERMISSIONS_REQUEST_CAMERA is an
// app-defined int constant. The callback method gets the
// result of the request.
}
} else {
setBarCodeScannerView();
}
}
以上方法检查是否已授予权限,如果没有,则检查该方法是否需要自定义解释。
ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA)
该方法的文档在此处 shouldShowRequestPermissionRationale()。此方法仅在用户拒绝权限对话框或用户从应用程序设置中关闭权限时返回 true,如果用户这样做,则显示带有自定义说明的警报对话框并继续进行,希望它能起作用。