Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Location Manager remove updates permission

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.

like image 584
Madhukar Hebbar Avatar asked Sep 22 '15 11:09

Madhukar Hebbar


People also ask

What are the required permissions for android LocationManager?

Android offers two location permissions: ACCESS_COARSE_LOCATION and ACCESS_FINE_LOCATION . The permission you choose determines the accuracy of the location returned by the API. android.

What is LocationManager in android?

android.location.LocationManager. This class provides access to the system location services. These services allow applications to obtain periodic updates of the device's geographical location, or to be notified when the device enters the proximity of a given geographical location.


1 Answers

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)
like image 109
Tim Avatar answered Sep 17 '22 23:09

Tim