8

I am using android studio and compileSdkVersion is 23 in that i am using below code

 if(locationManager != null){
            locationManager.removeUpdates(GPSListener.this);
        }

to stop gps update where GPS Listener is a class which implements LocationListener.

but in removeUpdates line i am getting below lint warning

Call requires permission which may be rejected by user: code should explicitly check to see if permission is available (with checkPermission) or handle a potential SecurityException

I am not getting what is the issue in the above code. Any extra permission need to be added in manifest file?.

Regards.

Madhukar Hebbar
  • 3,113
  • 5
  • 41
  • 69

4 Answers4

26

Since SDK 23, you should/need to check the permission before you call Location API functionality. Here is an example of how to do it:

if (locationManager != null) {
    if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
            || checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
        locationManager.removeUpdates(GPSListener.this);
    }
}

There is checkSelfPermission(), which is to check if 'you' (this app) has the correct permissions. There is also checkPermission(), which is to check if another process has the correct permissions.

Notes

  • next to doing this runtime check, it is still also necessary to require the relevant permissions in the AndroidManifest.
  • if your targetSdk is < 23, you should use ContextCompat.checkSelfPermission() instead (thanks to JerryBrady)
Tim
  • 41,901
  • 18
  • 127
  • 145
  • 8
    Note that if your target is < 23, you should use ContextCompat.checkSelfPermission() instead. – Jerry Brady Oct 30 '15 at 18:44
  • I want to cry. Why would the app need a permission to remove an already attached locationlistener from the location manager? – Glenn Bech Feb 25 '17 at 05:30
14

I wasn't able to use checkSelfPermission(), because my min API is 14 and 23 is required. Knowing that, you can also try to catch a SecurityException.

Example:

try {
    locationManager.removeUpdates(GPSListener.this);
} catch (SecurityException e) {
    Log.e("PERMISSION_EXCEPTION","PERMISSION_NOT_GRANTED");
}
Artjom B.
  • 61,146
  • 24
  • 125
  • 222
Rodrigo Borba
  • 1,334
  • 1
  • 14
  • 21
  • Question is for the target sdk version 23. There is no relation with min api. – Madhukar Hebbar Oct 30 '15 at 06:15
  • 2
    Question was for "compileSdkVersion 23" which does not mean the app is targeting 23. If you want backward compatibility with full Android M support, use ContextCompat.checkSelfPermission() instead and it will work. – Jerry Brady Oct 30 '15 at 18:44
  • 1
    I'm sorry. I didn't realize that. But I think that this option using try - catch still useful. – Rodrigo Borba Nov 03 '15 at 00:23
  • 1
    This answer is helped me. But I think this was not a proper solution. But I upvoted it. – Sai's Stack Dec 23 '15 at 02:40
5

To add to Jerry Brady's comment regarding ContextCompat, this is be the full code for < 23:

 if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
                || ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED)
-1

Here is my solution !

  if (Build.VERSION.SDK_INT >= 23) {

                if (checkSelfPermission(android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
                        || checkSelfPermission(android.Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {

                    lm.removeUpdates(this);
                }
            }
            else
            {
                lm.removeUpdates(this);

            }
toto_tata
  • 14,526
  • 27
  • 108
  • 198