How do I unsubscribe a LocationListener
from recieving updates from the LocationManager
?
Here is how I'm setting it up
mLocationManager = (LocationManager)this.getSystemService(LOCATION_SERVICE);
mListener = new LocationListener() {
public void onLocationChanged(Location location) {
Log.i("LocationListener", "Logging Change");
}
}
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
5000, 1, mListener);
After I have exited the the view that created the LocationListener
I am still getting log messages in the LogCat window.
I understand that this is because I am orphaning the listener but I cannot see any destroy method on the LocationListener
nor can I see any "remove listener" style methods on the LocationManager
object.
android.location.LocationListener. Used for receiving notifications when the device location has changed. These methods are called when the listener has been registered with the LocationManager.
Coming to your question, onLocationChanged() will be called if the current location update is not matching with last known location.
removeUpdates(locationListenerObject); mLocManager = null; Call this inside your onLocationChange Method when lat and long has been captured by the listener. Show activity on this post. Show activity on this post.
Once you have Location object, you can use Geocoder. getFromLocation() method to get an address for a given latitude and longitude. This method is synchronous, and may take a long time to do its work, so you should call the method from the doInBackground() method of an AsyncTask class.
Call removeUpdates on LocationManager, passing your location listener.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With