How to change denied/granted permission in Android

2019-02-27 06:08发布

How can I give the user a chance to change his permissions in the App, which he has already set to granted/denied?

Let's say a user denied a permission. Later he want's to grant this permission. Is it possible? How can I do that?

4条回答
smile是对你的礼貌
2楼-- · 2019-02-27 06:39

You show user a dialog asking to go to application settings and change the permission there, you have to motivate him somehow, tell why you need the permission. And you put an "Open settings" buton which triggers following function:

private void startInstalledAppDetailsActivity() {
    final Intent i = new Intent();
    i.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
    i.addCategory(Intent.CATEGORY_DEFAULT);
    i.setData(Uri.parse("package:" + getPackageName()));
    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    i.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
    i.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
    startActivity(i);
}
查看更多
爷的心禁止访问
3楼-- · 2019-02-27 06:43

Yes you can do that.

There is callback if onRequestPermissionsResult in which you get to know if user has granted permission or not. If user has not granted permission you can again request for the same whenever required.

查看更多
我只想做你的唯一
4楼-- · 2019-02-27 06:46

In case user has denied the permission you can check for the permission and if he also has checked never show again, in that case you can open your own dialog explaining the need of that permission for your app and from that dialog you can take the user to the app settings where he can allow the permission if he wanted.

public static void checkPermissionForExternalStorage(final Context mContext) {

    if (Build.VERSION.SDK_INT >= 23) {

        int writeExternalStorage = ContextCompat.checkSelfPermission(mContext, Manifest.permission.WRITE_EXTERNAL_STORAGE);
        if (writeExternalStorage != PackageManager.PERMISSION_GRANTED) {

            ActivityCompat.requestPermissions((Activity) mContext, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_CODE_ASK_PERMISSIONS_FOR_STORAGE);
            return;
        }

        //Do your stuff

    } else {

       //Do your stuff
    }
}



 public static void startInstalledAppDetailsActivity(Context mContext) {

    Intent i = new Intent();
    i.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
    i.addCategory(Intent.CATEGORY_DEFAULT);
    i.setData(Uri.parse("package:" + mContext.getPackageName()));
    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    i.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
    i.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
    mContext.startActivity(i);
}



   @Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {

    switch (requestCode) {



        case REQUEST_CODE_ASK_PERMISSIONS_FOR_STORAGE:
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                //Do your stuff
            } else {
                // Permission Denied

                if (!ActivityCompat.shouldShowRequestPermissionRationale((Activity) mContext, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                    showMessageOKCancel("The app was not allowed to write to your storage. Hence, it cannot function properly. Please consider granting it this permission. Tap Settings > Permissions, and turn Storage on.",
                            new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    startInstalledAppDetailsActivity((Activity) mContext);
                                }
                            });
                }


                Toast.makeText( mContext, "Write to external storage permission request denied.", Toast.LENGTH_SHORT).show();
            }
            break;
        default:
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}
查看更多
祖国的老花朵
5楼-- · 2019-02-27 06:57

There are multiple ways to achieve what you are looking for:

  1. You can create a separate View/Fragment just for this purpose enlisting all of the permissions that your app requires, from there you can call requestPermission().

  2. Or you can simply prompt user for permission whenever user opens the segment of the app that requires permission to run by calling requestPermission().

  3. User can also grant permission to the app from Settings. So It might be a good idea to direct user (via a dialog for instance) to change permission at a later time if he so wishes from Settings.

查看更多
登录 后发表回答