gyamana gyamana - 1 year ago 94
Android Question

ACCESS_FINE_LOCATION SecurityException despite specifying the permission in the manifest file

My app is attempting to access the device's location and I have included the following in the


<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<meta-data android:name="" />


I have implemented the
as follows to access the location service:

public class BackgroundLocationService implements
GoogleApiClient.OnConnectionFailedListener {

private static final String TAG = BackgroundLocationService.class.getSimpleName();

private static GoogleApiClient googleApiClient;
private static PendingIntent locationCallback;

public static int LOCATION_INTERVAL = 10000;
public static int FAST_INTERVAL = 5000;

public void onConnected(Bundle bundle) {
Log.i(TAG, "Connected to Google API");

LocationRequest request = new LocationRequest();

LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, request, locationCallback);

public void onConnectionSuspended(int i) {
Log.i(TAG, Integer.toString(i));

public void onConnectionFailed(ConnectionResult connectionResult) {
Log.i(TAG, connectionResult.toString());

When I trigger the location service call, the following exception is thrown:

java.lang.SecurityException: Client must have ACCESS_FINE_LOCATION permission to request PRIORITY_HIGH_ACCURACY locations.
at android.os.Parcel.readException(
at android.os.Parcel.readException(
at$zza$zza.zza(Unknown Source)
at Source)
at Source)
at$7.zza(Unknown Source)
at$7.zza(Unknown Source)
at$zza.zzb(Unknown Source)
at Source)
at Source)
at Source)
at Source)
at com.localz.spotzpush.sdk.service.BackgroundLocationService.onConnected(
at Source)
at Source)
at Source)
at$2.onConnected(Unknown Source)
at$zzg.zzpf(Unknown Source)
at$zza.zzc(Unknown Source)
at$zza.zzt(Unknown Source)
at$zzc.zzph(Unknown Source)
at$zzb.handleMessage(Unknown Source)
at android.os.Handler.dispatchMessage(
at android.os.Looper.loop(
at java.lang.reflect.Method.invoke(Native Method)

I am using an actual device to produce this issue and have been scratching my head on why the exception is thrown.

Answer Source

The issue was caused by a new feature for Android 6.0.

Beginning in Android 6.0 (API level 23), users grant permissions to apps while the app is running, not when they install the app.

To get around the issue when the permission is not explicitly granted, I had put in a check to wrap location service calls (as per the Google Documentation, slightly modified).

int permissionCheck = ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION);

if (permissionCheck == PackageManager.PERMISSION_GRANTED) {
    //Execute location service call if user has explicitly granted ACCESS_FINE_LOCATION..

The Google Documentation also steps through how to request for the permissions that are needed.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download