为 Android M 权限对话框提供自定义文本

Provide custom text for Android M permission dialog

是否可以为要求用户授予权限时显示的系统对话框提供自定义文本?

不,您不能自定义对话框的文本,但您可以在请求权限之前提供解释。引用自 developer.android.com:

Request Permissions

If your app needs a dangerous permission that was listed in the app manifest, it must ask the user to grant the permission. Android provides several methods you can use to request a permission. Calling these methods brings up a standard Android dialog, which you cannot customize.

Explain why the app needs permissions

In some circumstances, you might want to help the user understand why your app needs a permission. For example, if a user launches a photography app, the user probably won't be surprised that the app asks for permission to use the camera, but the user might not understand why the app wants access to the user's location or contacts. Before you request a permission, you should consider providing an explanation to the user. Keep in mind that you don't want to overwhelm the user with explanations; if you provide too many explanations, the user might find the app frustrating and remove it.

One approach you might use is to provide an explanation only if the user has already turned down that permission request. If a user keeps trying to use functionality that requires a permission, but keeps turning down the permission request, that probably shows that the user doesn't understand why the app needs the permission to provide that functionality. In a situation like that, it's probably a good idea to show an explanation.

To help find situations where the user might need an explanation, Android provides a utiltity method, shouldShowRequestPermissionRationale(). This method returns true if the app has requested this permission previously and the user denied the request.

我们无法自定义请求权限对话框,但我们可以为用户提供自定义解释,说明为什么我们在下面请求是带有自定义解释的方法

   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()此方法 return仅当用户拒绝权限对话框或用户从应用程序设置中关闭权限时才为真,如果用户这样做则显示警报与自定义解释对话并继续希望它有效