16

I have been looking at and playing with the FusedLocationProviderApi. That class contains these two methods:

PendingResult<Status> requestLocationUpdates(GoogleApiClient client, LocationRequest request, LocationCallback callback, Looper looper)

PendingResult<Status> requestLocationUpdates(GoogleApiClient client, LocationRequest request, LocationListener listener, Looper looper)

They have very similar signatures and descriptions with the only difference being that one uses LocationCallback and the other uses LocationListener.

The abstract class LocationCallback defines two methods:

void onLocationAvailability(LocationAvailability locationAvailability)

void onLocationResult(LocationResult result)

and the interface LocationListener defines just one method

abstract void onLocationChanged(Location location)

Disregarding the additional method in LocationCallback, what is the difference between these two? Is there some conceptual difference or special use case that makes one preferable over the other? What is the rationale in duplicating the functionality?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
f470071
  • 1,527
  • 3
  • 18
  • 30

1 Answers1

19

You'll note that onLocationResult() returns a LocationResult - this makes it a lot easier to deal with receiving multiple locations simultaneously - a case you'll run into quite often if you're properly batching location requests by setting setMaxWaitTime().

If you opt to use the older LocationListener, you'll receive multiple callbacks to onLocationChanged() in a row when batching.

ianhanniballake
  • 191,609
  • 30
  • 470
  • 443
  • Hmm. This batching looks interesting. If I get this right I can make a LocationRequest with interval of 10 sec and set MaxWaitTime to 100 seconds and then get all 10 (or more or less) locations in one LocationResult. Could be useful. Upvoted. – f470071 Jul 28 '16 at 19:33
  • 3
    Yep! And on many modern phones, the batching is done directly on the GPS chip, meaning the main CPU doesn't need to wake up at all between batches, leading to some pretty impressive battery improvements. – ianhanniballake Jul 28 '16 at 19:35
  • 2
    I do love short answers that tell me exactly what I needed to know! – Opus1217 Aug 14 '17 at 19:27
  • I am using locationcallback and i want to get location updates every single second and for that i have added interval of 1 sec in locationrequest object as `locationRequest = new LocationRequest() .setInterval(1000) .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY) .setFastestInterval(1000);` but still i am getting location update every 10 sec. Can you please explain me why it happens? @ianhanniballake – Jaydip Kalkani Mar 11 '18 at 04:50
  • @JaydipKalkani - the `setInterval` docs specifically say "This interval is inexact. You may not receive updates at all (if no location sources are available), or you may receive them slower than requested" – ianhanniballake Mar 11 '18 at 04:57
  • Don't you think it should return location at least every 5 sec if i have setted interval of 11 sec? @ianhanniballake – Jaydip Kalkani Mar 11 '18 at 05:13
  • and if you want to see my whole code how i am requesting updates then you can see my [this](https://stackoverflow.com/questions/49115524/should-i-need-to-unregister-locationcallback-for-fusedlocationproviderclient) question. @ianhanniballake – Jaydip Kalkani Mar 11 '18 at 05:17