Stop Location Listener in Android
Solution 1
mLocManager.removeUpdates(locationListenerObject);
mLocManager = null;
Call this inside your onLocationChange
Method when lat and long has been captured by the listener.
Hope I helped..
Solution 2
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.
Related videos on Youtube
Chrishan
Experienced Android Developer. Please find my profile at LinkedIn
Updated on July 09, 2022Comments
-
Chrishan almost 2 years
In my android app I need to get current GPS location of the user at the start of the application only if location has change from the previous location. But the problem is when I inside the application if location change (ie If user was traveling while using the app) app is starting from the beginning.
I need to stop location listener after getting user current location. removeUpdates method didn't work for me.
Please help me on this issue. thanks in advance !
-
PravinCG almost 13 yearsremoveUpdates is the way to go, can you show some code as to why it did not work.
-
-
AbhishekB over 10 yearsIt might take some time for the onLocationChange method to be called. Around 1 sec might be. Right?
-
rockhammer over 7 yearsthx this works. the weird thing is that the .removeUpdates() wants us to ask for permissions!
-
rockhammer over 7 yearsI just tried this on a actual device and it does not remove the listening for updates
-
Ruchir Baronia almost 7 yearsOut of curiosity, why is setting the listener to null necessary if we already called
removeUpdates(locationListener l)
?