0

I have an Intent Service that is invoked by FusedLocationApi every minute or so to monitor locations periodically via pending .

@Override
protected void onHandleIntent(Intent intent) {
        if (intent != null && LocationResult.hasResult(intent)) {

            LocationResult locationResult = LocationResult.extractResult(intent);
            location = locationResult.getLastLocation();

        } else {
            Crashlytics.logException(new LocationException("Loc Intent Service invoked without location", new NullPointerException()));
        }
}

It is working fine most of the time but in production, in the last 7 days I have received 13,000 events of the service being invoked by the FusedLocationApi without any location. (thanks to the Crashlytics non fatal exception I've added)

Why or when would this happen? I have tried disabling location permission while the locations are being monitored, in which case the service is invoked without location but just once or twice, after which it is never invoked again until I turn location permission back on.

Ishaan Garg
  • 3,014
  • 3
  • 25
  • 28
  • You are never guaranteed to receive a location in any case, you always need to check to see if the location is null or not. – tyczj Jul 14 '17 at 11:47

1 Answers1

-1

Per the requestLocationUpdates() documentation:

Both LocationResult and LocationAvailability are sent to the given PendingIntent. You can extract data from an Intent using hasResult(Intent), extractResult(Intent), hasLocationAvailability(Intent), and extractLocationAvailability(Intent).

While you're checking the first case, you're not handling (or ignoring) changes in the location availability.

ianhanniballake
  • 191,609
  • 30
  • 470
  • 443