Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference LocationListener LocationCallback?

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?

like image 874
f470071 Avatar asked Jul 28 '16 19:07

f470071


People also ask

What is LocationCallback?

Interface LocationCallback A LocationCallback is used to run code after a Location has been fetched by com. parse. ParseGeoPoint#getCurrentLocationInBackground(long, android. location.Criteria) .

How to use requestLocationUpdates in Android?

Make a location request The lesson on Changing Location Settings shows you how to do this. Once a location request is in place you can start the regular updates by calling requestLocationUpdates() . Depending on the form of the request, the fused location provider either invokes the LocationCallback.

How do I turn off location listener on Android?

You can stop the LocationListener by making its object to null after stoping LocationListener locationManager. removeUpdates(mLocListener); , that is mLocListener = null; when you want it to stop fetching the Latitudes and Longitudes. You can stop continuous location updating by calling method locationManager.

How can we implement location listener in Android?

The LocationListener class needs to implement the following methods. onLocationChanged(Location location) : Called when the location has changed. onProviderDisabled(String provider) : Called when the provider is disabled by the user. onProviderEnabled(String provider) : Called when the provider is enabled by the user.


1 Answers

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.

like image 63
ianhanniballake Avatar answered Oct 03 '22 23:10

ianhanniballake