10

In Shot:, In onRequestPermissionsResult grantResults on some device return empty and some device have a value PackageManager.PERMISSION_DENIED when user decline the permission.

I have implmeneted a solution for identify user has selected accepted ,deny and deny with don't ask again for run time permission,based on https://stackoverflow.com/a/31925748/2941375 answer.

as per many docs i have seen,if user decline permission then it returns grantResults empty

Code i have used else if (grantResults[0] == PackageManager.PERMISSION_DENIED) so in else if part it throw Arrayindexoutofbound exception

i have tested code when user decline permission grantResults is not emplty for my case,but i have seen crash report on fabric console for grantResults there is many crash with arrayindexoutofbound,

 @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case PermissionManager.MY_PERMISSIONS_REQUEST_LOCATION_ACCESS: {
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    DefineLocationService.start(this);
                    startNextActivity(0);
                } else if (grantResults[0] == PackageManager.PERMISSION_DENIED) {
                    boolean showRationale = ActivityCompat.shouldShowRequestPermissionRationale(this, permissions[0]);
                    if (!showRationale) {
                        // user also CHECKED "never ask again"
                        // you can either enable some fall back,
                        // disable features of your app
                        // or open another dialog explaining
                        // again the permission and directing to
                        // the app setting
                        startNextActivity(ARTIFICIAL_DELAY_MILLIS);
                    } else if (!PermissionManager.MY_REQUESTED_DIALOG) {
                        PermissionManager.checkLocationPermission(this);
                    } else {
                        startNextActivity(0);
                    }
                } else {
                    startNextActivity(ARTIFICIAL_DELAY_MILLIS);
                }


            }
        }
    }

can anyone have any explanation for this,why some device return grantResults empty and some device return grantResults have value with decline when user decline permission.

i have tested many time but grantResults never empty on my side,but there is crash on console,it means in some case it is empty and grantResults[0] throw the exception.

MJM
  • 5,119
  • 5
  • 27
  • 53

2 Answers2

14

As per the documentation:

Note: It is possible that the permissions request interaction with the user is interrupted. In this case you will receive empty permissions and results arrays which should be treated as a cancellation.

How you want to deal with cancellation is totally up to you (re-ask for permission, treat it as a denial, etc), so just make sure to account for that case in your code.

ianhanniballake
  • 191,609
  • 30
  • 470
  • 443
  • 1
    is there anyway,using that i can reproduce this issue? i mean interrupted the interaction with user. – MJM Jun 09 '18 at 05:13
  • You can rotate your device while the permission prompt is up. – ianhanniballake Jun 09 '18 at 05:51
  • 1
    @ianhanniballake That doesn't work, the permission prompt stays there. – Louis CAD May 01 '19 at 10:49
  • The only way I found to reproduce it is with "Don't keep Activities" enabled, but then, all subsequent permission requests fail with empty `grantResults` coming. So the "totally up to you" doesn't really apply… Any help to deal with it properly is more than welcome. – Louis CAD May 01 '19 at 20:12
0

I faced the same issue when the TargetSdk version was lower than required for the permission. (In my case it was POST_NOTIFICATIONS). Once I changed it to API version 33 everything started to work as expected.

lobzik
  • 10,974
  • 1
  • 27
  • 32