App Keeps Pinging GPS
-
28-09-2019 - |
Question
I have a mapView
activity that has a LocationListener
registered to check for a new location every two seconds. When I press the back button to go back to the main screen of the app the GPS continues to check for a location every two seconds. Does the listener not get stopped when the Activity
stops? If not how can I handle this? I currently have not overridden onStop()
.
private final LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
updateWithNewLocation(location);
}
public void onProviderDisabled(String provider) {
//TODO do something to handle when the provider is disabled
}
public void onProviderEnabled(String provider) { }
public void onStatusChanged(String provider, int status, Bundle extras) { }
};
Solution
You need to override the onPause
method of your Activity
because that is sure to be called.
In the onPause
method you should unregister the LocationListener
.
You do this through
locationManager.removeUpdates(yourListener);
If you have a mapView
I sometimes had the problems with the locationOverlay
keeping a listener registered and using the GPS even if the mapview was in the background.