1

I have a query implementing RuntimePermission for Location. When I tried to requestLocationUpdates, I got LintError suggesting me to add PermissionCheck for that line. Considering that I implemented run-time permissions. So this is how it looks,

if (isNetworkEnabled() && networkListener != null) {
        if (ActivityCompat.checkSelfPermission(context,
                Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
                ActivityCompat.checkSelfPermission(context,
                        Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

            ActivityCompat.requestPermissions(activity, new String[]
                    {Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_LOCATION);

        } else
            mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, networkListener);
}

And my main class implements onRequestPermissionsResult callback. This looks like,

switch (requestCode) {
        case REQUEST_LOCATION:
                if (grantResults.length == 2 && grantResults[0] == PackageManager.PERMISSION_GRANTED
                        && grantResults[1] == PackageManager.PERMISSION_GRANTED) {
                        mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, networkListener);
                } else
                    Message.displayToast(context, "Without enabling permission, you can't access this feature");
            break;
    }

After the permission is granted, I request location updates again. But it again shows LintError to add the PermissionCheck. Refer the below image

enter image description here

Just for try I checkSelfPermission before requesting for requestLocationUpdate inside onRequestPermissionsResult and the error is gone. Like below code.

if (ActivityCompat.checkSelfPermission(context, permissions[0]) == PackageManager.PERMISSION_GRANTED &&
                            ActivityCompat.checkSelfPermission(context, permissions[1]) == PackageManager.PERMISSION_GRANTED)

So, my question is do I need to check the permission once again if the user granted the permission? Correct me if I'm wrong!

Kavin Prabhu
  • 2,307
  • 2
  • 17
  • 36
  • But why this is needed? After permission is granted, I'm requesting for update! Just trying to understand the use case. – Kavin Prabhu Feb 10 '16 at 09:46
  • 1
    Lint knows that any call that requires user permission must be wrapped around a permission check statement and thus the Lint error. You can execute this program without any issue if you know that the exception won't be thrown. – Much Overflow Feb 10 '16 at 09:52
  • I would put the permission check and location update logic inside a function named `checkAndCallLocationUpdates` and call it from the `onRequestPermissionResult` – Much Overflow Feb 10 '16 at 09:55
  • 1
    I even tried by having a common function. But I have some local variables which cannot be acquired from `onRequestPermissionsResult` so I dropped it. But it was a query just to understand why it's like this. If this is the case I have to check it again. Thanks for your quick reply. Appreciate it – Kavin Prabhu Feb 10 '16 at 10:02
  • 1
    http://stackoverflow.com/questions/35124794/android-studio-remove-security-exception-warning – Jyotman Singh Feb 10 '16 at 10:12

1 Answers1

2

You do need to check for checkSelfPermission because with latest OS 6 (Marshmallow) you can revoke the permissions granted for an app by going into settings.
So even if user has granted the permissions to app during installation time, at runtime you need to doublecheck whether your app still has the permissions or user has revoked those permissions.

Saurabh7474
  • 450
  • 5
  • 12