Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unsubscribing a LocationListener from the LocationManager

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.

like image 743
Greg B Avatar asked Jan 19 '10 22:01

Greg B


People also ask

What is LocationListener in Android?

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.

When onLocationChanged is called in Android?

Coming to your question, onLocationChanged() will be called if the current location update is not matching with last known location.

How do I turn off Location listener on Android?

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.

Which method is invoked when a new location is available?

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.


1 Answers

Call removeUpdates on LocationManager, passing your location listener.

like image 87
Mark B Avatar answered Sep 28 '22 08:09

Mark B