简体   繁体   中英

How to detect if user disable granted permissions in the settings in android?

In android 6.0 and above the permissions granted at run time and the user can cancel the granted permissions by going to the settings.

I want to run some code just after the user cancels the granted permissions by going to the settings. Is there anyway to do it?

// call this for checking the permission before other Activity or function //to call

if (hasPermission()) {
// do thing

} else {
requestPermissionDialog();
}

/////////////////hasPermission////////////////////////////

private boolean hasPermission() {
boolean permission = true;
if (ActivityCompat.checkSelfPermission(WelcomeActivity.this, 
Manifest.permission.WRITE_EXTERNAL_STORAGE) != 
PackageManager.PERMISSION_GRANTED) {
permission = false;
}
return permission;
}

//////////////////////////requestPermissionDialog///////////////////////

private void requestPermissionDialog() {

    permissionStatus = getSharedPreferences("permissionStatus", MODE_PRIVATE);


    if (ActivityCompat.checkSelfPermission(WelcomeActivity.this, Manifest.permission.WRITE_Eenter code hereXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
        if (ActivityCompat.shouldShowRequestPermissionRationale(WelcomeActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
            //Show Information about why you need the permission
            //  resetPasswordDialog(WelcomeActivity.this,"ok");

            AlertDialog.Builder builder = new AlertDialog.Builder(WelcomeActivity.this);
            builder.setInverseBackgroundForced(true);
            builder.setTitle("Storage Permission");

            builder.setMessage("This app needs Storage permission.");
            builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                    ActivityCompat.requestPermissions(WelcomeActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, EXTERNAL_STORAGE_PERMISSION_CONSTANT);
                }
            });
            builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });
            builder.show();
        } else if (permissionStatus.getBoolean(Manifest.permission.WRITE_EXTERNAL_STORAGE, false)) {
            //Previously Permission Request was cancelled with 'Dont Ask Again',
            // Redirect to Settings after showing Information about why you need the permission


            // resetPasswordDialog(WelcomeActivity.this,"settings");
            AlertDialog.Builder builder = new AlertDialog.Builder(WelcomeActivity.this);
            builder.setInverseBackgroundForced(true);

            builder.setTitle(" Storage Permission");
            builder.setMessage("This app needs Storage permission.");
            builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                    sentToSettings = true;
                    Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                    Uri uri = Uri.fromParts("package", getPackageName(), null);
                    intent.setData(uri);
                    startActivityForResult(intent, REQUEST_PERMISSION_SETTING);
                    Toast.makeText(getBaseContext(), "Go to Permissions to allow Storage Audio ", Toast.LENGTH_LONG).show();
                }
            });
            builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });
            builder.show();
        } else {
            //just request the permission
            ActivityCompat.requestPermissions(WelcomeActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, EXTERNAL_STORAGE_PERMISSION_CONSTANT);
        }

        SharedPreferences.Editor editor = permissionStatus.edit();
        editor.putBoolean(Manifest.permission.WRITE_EXTERNAL_STORAGE, true);
        editor.commit();

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM