3

I am making an application which require storage permission, I request permission using requestPermission(). this gives user the rights to turn them off from setting, but I want the user to know that it is necessary to give permission to the app's basic feature. I want this alert dialog to be displayed when the user is disabling the App permissions from the settings. Is that possible?

enter image description here

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Deepanshu Namdeo
  • 159
  • 1
  • 12

2 Answers2

2

when the user is disabling the App permissions from settings is that possible?

Unfortunately its not possible. You cannot detect it. Best you can do is to check for permission before using features that requires this permission.

When your app is in background

Display a notification indicating missing permission. When user clicks on the notification:

  • Redirect him/her to activity where you can request the permission
  • Redirect him/her to settings screen.

When your app is in foreground

you can show the permission rationale to highlight the importance of the permission. Android provides a utility method, shouldShowRequestPermissionRationale(), that returns true if the user has previously denied the request. When this method returns trueshow the rationale.

Reason why you are seeing the dialog from settings screen

You are seeing the dialog because the application for which you are disabling the permission has targetSDkversion < Android M. System has enabled the compatibility mode to grant the permission at install time but since the app's version is not compatible with current OS it warns user regarding it.

Sagar
  • 23,903
  • 4
  • 62
  • 62
  • Is there any way to do that ? another replacement or library ? – Arash Hatami Jun 08 '18 at 07:01
  • @ArashHatami you mean show the permission when user is interacting with your app? – Sagar Jun 08 '18 at 07:02
  • No ... I want show permission even disabled from settings – Arash Hatami Jun 08 '18 at 07:03
  • Android system apps like contacts have this kind of Alert coming when disabling storage permission – Deepanshu Namdeo Jun 08 '18 at 07:03
  • 1
    @ArashHatami You can't. Best you can do is to periodically check for the permissions and present notification to indicate to enable the permission – Sagar Jun 08 '18 at 07:04
  • and i have A background service which is storing device information in file that too require storage permission ,now when user disable the storage permission ,in what ways we can handle this type of situation. – Deepanshu Namdeo Jun 08 '18 at 07:08
  • @DeepanshuNamdeo yes. When your service is attempting to write, it can check if permission is granted. If not then you can show the notification asking user to grant this permission – Sagar Jun 08 '18 at 07:10
  • but service will require activity context to display the alert box – Deepanshu Namdeo Jun 08 '18 at 07:11
  • @DeepanshuNamdeo You have to show the notification. When user clicks on Notification, redirect him/her to activity which will request for permission – Sagar Jun 08 '18 at 07:12
  • @DeepanshuNamdeo also consider limitation implied on background service from Android O. You can check my answer on this [SO](https://stackoverflow.com/questions/49566426/can-i-still-have-a-data-logging-background-service-in-android-8-0/49568651#49568651) for it. – Sagar Jun 08 '18 at 07:13
0

Example of STORAGE PERMISSION REQUEST

Requesting permission

private int STORAGE_PERMISSION = 1;
private AlertDialog alertDialog;

if (!(ActivityCompat.checkSelfPermission(LauncherActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED)) {
            ActivityCompat.requestPermissions(LauncherActivity.this, new
      String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, STORAGE_PERMISSION);
}

After request of permission. Activity will return to this method

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (requestCode == STORAGE_PERMISSION && (ActivityCompat.checkSelfPermission(LauncherActivity.this,Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED)){
     // when user denied the permission
     // show dialog to user that the permission is mandatory and add positiveButton. inside the button recall permission check.

            alertDialog();
  }
}

Method responsible for alert dialog

private void alertDialog(){

    AlertDialog.Builder builder = new AlertDialog.Builder(this);

    builder.setTitle("your title").setMessage("your message for user to request to accept permissions");
        builder.setPositiveButton("Accept", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                if (!(ActivityCompat.checkSelfPermission(LauncherActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED)) {
                    ActivityCompat.requestPermissions(LauncherActivity.this, new
                String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, STORAGE_PERMISSION);
        }
            }
        });

    alertDialog = builder.create();
        alertDialog.show();
}
Chethan Kumar
  • 185
  • 1
  • 12