How to stop location manager?

Tima picture Tima · Nov 16, 2010 · Viewed 43.3k times · Source

Don't know why, but sometimes LocationManager is still working also after closing application.

I call startGPS() in onCreate-Methode in one Activity (only one, let me call it StartActivity).

protected void startGPS(){    
 try {           
     lmanager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
     lmanager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
     lmanager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
 } catch(Exception e) {
     e.printStackTrace();
 }
}

And if this activity will be destroyed (so, when application will be closed), I call endGPS()

public void endGPS(){
 try {           
     lmanager.removeUpdates(this);
     lmanager=null;
 } catch(Exception e) {
  e.printStackTrace();
 }
}

Some ideas, some suggestions, what have I done wrong?!

Answer

bpz picture bpz · Jul 17, 2014

You should call the method removeUpdates inside the method onPause:

@Override
protected void onPause() {
    super.onPause();
    locationManager.removeUpdates(this);
    Log.i(TAG, "onPause, done");
}