Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android permissions - "Never ask again" selection is remembered forever

My app asks the user for camera permission. If the user rejects the permission, he is asked for permission at the restart again. This time, "never ask again" checkbox is shown to the user. If the user selects the never ask again checkbox, the application can never ask for permission. It is OK. The problem is that my phone does never forget this selection, it remembers this selection. I removed the application, cleared all data but nothing works.

When I delete the application and reinstall it, the application can still never ask for permission. I opened Settings-> Application Settings -> Then I gave necessary permission to my application manually. It is ok. Then, I delete and reinstall it again. But the application can still never ask for permission. I do not want to give my app permission manually at each installation. How to reset the selection of "never ask again" checkbox.

if (Build.VERSION.SDK_INT >= 23)
{
    int hasPermission = ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CAMERA);

    if (hasPermission != PackageManager.PERMISSION_GRANTED)
    {
        if (shouldShowRequestPermissionRationale(Manifest.permission.CAMERA))
        { 
            getErrorDialog("You need to allow Camera permission." +
                    "\nIf you disable this permission, You will not able to add attachment.", MainActivity.this, true).show();
        }
        return;
    }
}

public AlertDialog.Builder getErrorDialog(String message, Context context, final boolean isFromCamera) {
   final AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
   alertDialog.setTitle(getString(R.string.app_name)).setMessage(message);
   alertDialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
       @Override
       public void onClick(DialogInterface dialog, int which) {

           dialog.dismiss();
           if (Build.VERSION.SDK_INT >= 23) {
               if(isFromCamera){
                   requestPermissions(new String[]{Manifest.permission.CAMERA},
                           REQUEST_CODE_ASK_PERMISSIONS_CAMERA);
               }else {
                   requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                           REQUEST_CODE_ASK_PERMISSIONS_EXTERNAL_STORAGE);
               }
           }
       }
   });
   return alertDialog;
}
like image 934
metis Avatar asked Jan 17 '18 12:01

metis


2 Answers

You can't change this behaviour (from Mark Murphy's book):

If the user checks that and clicks the Deny button, not only will you not get the runtime permission now, but all future requests will immediately call onRequestPermissionsResult() indicating that your request for permission was denied. The only way the user can now grant you this permission is via the Settings app.

And what you can do in this situation is the following:

For permissions that, when denied, leave your app in a completely useless state, you may wind up just displaying a screen on app startup that says “sorry, but this app is useless to you”, with options for the user to uninstall the app or grant you the desired permissions. Note that shouldShowRequestPermissionRationale() returns false if the user declined the permission and checked the checkbox to ask you to stop pestering the user.

Hence, as Jaymin correctly noted, your best choice is to handle this with shouldShowRequestPermissionRationale().

like image 109
Ksenia Avatar answered Oct 02 '22 00:10

Ksenia


Adding the below else block solved my problem.

if (Build.VERSION.SDK_INT >= 23)
{
     int hasPermission = ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CAMERA);
     if (hasPermission != PackageManager.PERMISSION_GRANTED)
     {
         if (shouldShowRequestPermissionRationale(Manifest.permission.CAMERA))
         { 
             getErrorDialog("You need to allow Camera permission." +
                     "\nIf you disable this permission, You will not able to add attachment.", MainActivity.this, true).show();
         }
         else
         { 
             requestPermissions(new String[]{Manifest.permission.CAMERA}, REQUEST_CODE_ASK_PERMISSIONS_CAMERA);

         }
         return;
     }
 }
like image 32
metis Avatar answered Oct 01 '22 23:10

metis