How to change denied/granted permission in Android M?

前端 未结 4 1629
清酒与你
清酒与你 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: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);
        }
    }
    

提交回复
热议问题