How to change denied/granted permission in Android M?

前端 未结 4 1625
清酒与你
清酒与你 2021-01-22 02:12

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\

相关标签:
4条回答
  • 2021-01-22 02:37

    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.

    0 讨论(0)
  • 2021-01-22 02:41

    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);
    }
    
    0 讨论(0)
  • 2021-01-22 02:43

    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);
        }
    }
    
    0 讨论(0)
  • 2021-01-22 02:54

    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.

    0 讨论(0)
提交回复
热议问题