Search code examples
androidgpsupdatesgeo

The GPS updates won't turn off after closing application


I have created an application that has a 'geolocation' feature responsible for spotting a user on the Google map like many other applicatons. I used "LocationManager.NETWORK_PROVIDER" to locate the user and at the same time I instantiate and start "MyLocationOverlay" (in the onLocationChanged() method) to get the location. Because of the second one, the GPS turns on (blinking on the top) which is OK.

The problem is, after the application is closed (back button or through task manager), the GPS feature is still hanging there, trying to get the updates.

How to turn it off after the user leaves the activity? I tried suggestions from here and other forums like putting locationManager.removeUpdates(this); and locationManager.removeUpdates(mMyLocationOverlay); within the methods onPause(), onStop(), onDestroy(). The method OnPause looks like this:

@Override
protected void onPause() { 
  super.onPause(); 
  locationManager.removeUpdates(this);
  locationManager.removeUpdates(mMyLocationOverlay);
} 

('this' references my class that implements LocationListener)

Please, can someone help me to turn off GPS updates after leaving the activity (it's a class that extends MapActivity) but not turn off the GPS feature on the phone itself?

Interesting thing is that when I remove the part with MyLocationOverlay, GPS will not start of course and therefore no problem. So I'm pretty sure that mMyLocationOverlay is the listener that "won't stop" and producing a problem.


Solution

  • If you want to close (or end) the application you can use

    System.exit(0);

    so when the application is closed, all the services you use will close.