How to catch SecurityException for location permissions

Viewed 1403

I'm getting the following crash on certain devices API 6.0+:

Fatal Exception: java.lang.SecurityException: Client must have ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission to perform any location operations.
       at android.os.Parcel.readException(Parcel.java:1693)
       at android.os.Parcel.readException(Parcel.java:1646)
       at com.google.android.gms.internal.zzed.zza(Unknown Source)
       at com.google.android.gms.internal.zzcda.zzdw(Unknown Source)
       at com.google.android.gms.internal.zzcdd.zzvQ(Unknown Source)
       at com.google.android.gms.internal.zzcdj.zzvQ(Unknown Source)
       at com.google.android.gms.location.zzf.zza(Unknown Source)
       at com.google.android.gms.internal.zzbaq.zza(Unknown Source)
       at com.google.android.gms.internal.zzbdd.zzb(Unknown Source)
       at com.google.android.gms.internal.zzbdd.zzqq(Unknown Source)
       at com.google.android.gms.internal.zzbdd.onConnected(Unknown Source)
       at com.google.android.gms.common.internal.zzaa.onConnected(Unknown Source)
       at com.google.android.gms.common.internal.zzn.zzj(Unknown Source)
       at com.google.android.gms.common.internal.zze.zzrj(Unknown Source)
       at com.google.android.gms.common.internal.zzi.zzrk(Unknown Source)
       at com.google.android.gms.common.internal.zzh.handleMessage(Unknown Source)
       at android.os.Handler.dispatchMessage(Handler.java:102)
       at android.os.Looper.loop(Looper.java:154)
       at android.os.HandlerThread.run(HandlerThread.java:61)

Note that I am asking the user for location permission before making any location related operation. But for some reason I can't reproduce, some devices seem to be reporting having location permission when I use:

boolean hasPermission = ActivityCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_GRANTED;

Because of that, I need to be able to catch the SecurityException and ignore it when it happens. Or perhaps figure out a way to tell the user to enable location permissions manually.

The crash above started to happen when I started to use the new APIs from the Play Services to request location using the FusedLocationProviderClient. It crashes when I try to do the following without location permissions grated, for example:

LocationServices.getFusedLocationProviderClient(this).getLocationAvailability().addOnFailureListener(myListener);

The failure listener does not send the crash to myListener unfortunately. Surrounding the above with try..catch doesn't work either. I.e., this doesn't work and the app crashes anyway throwing SecurityException:

try {
    LocationServices.getFusedLocationProviderClient(this)
        .getLocationAvailability()
        .addOnFailureListener(myListener);
} catch(Exception e) {
    Log.e(e);
}

This problem seems to be related to the following issues:

Any ideas?

1 Answers

Have you considered the fact, that some users revoke the permission after your app was launched? You should always check the permissions every time before calling any methods in FusedLocationProviderClient.

Related