34

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?!

Marlon
  • 1,501
  • 2
  • 16
  • 36
Tima
  • 12,040
  • 22
  • 74
  • 121
  • Schwiz's answer seems like the most likely solution. To confirm, add a debug log entry to your endGPS() method and see if they're called when the application is closed by reviewing LogCat in Eclipse. – Brian Nov 16 '10 at 18:29
  • 1
    How are you determining that "LocationManager is still working also after closing application"? Also, I am nervous about you calling `requestLocationUpdates()` twice for the same listener object. – CommonsWare Nov 16 '10 at 18:33
  • 1
    @CommonsWare I can see the gps-icon in status-bar. Should I use two different listeners?! I just read "You can also request location updates from both the GPS and the Network Location Provider by calling requestLocationUpdates() twice—once for NETWORK_PROVIDER and once for GPS_PROVIDER." here http://developer.android.com/guide/topics/location/obtaining-user-location.html – Tima Nov 16 '10 at 22:11

6 Answers6

31

You should call the method removeUpdates inside the method onPause:

@Override
protected void onPause() {
    super.onPause();
    locationManager.removeUpdates(this);
    Log.i(TAG, "onPause, done");
}
bpz
  • 330
  • 1
  • 3
  • 8
12

Is it possible your activity isn't being destroyed? i.e.: you hit the home button. Move your gps start/stop to onStart and onPause.

Darkhogg
  • 12,286
  • 4
  • 20
  • 24
Nathan Schwermann
  • 30,406
  • 15
  • 78
  • 89
  • I'll check it tomorrow, but I'm prette sure, that activity will be destroyed. Probably CommonsWare is right and I should use two listeners. But I haven't found any example – Tima Nov 16 '10 at 22:16
8

The emulator never gets rid of the GPS icon once loaded. Hence, on an emulator, you cannot use the GPS icon as a test as to whether GPS is still running. On a device, though, the icon should vanish.

Should I use two different listeners?

I sure would. I do not know whether removeUpdates() will remove both, or even if both requests are registered with the single listener.

CommonsWare
  • 910,778
  • 176
  • 2,215
  • 2,253
5

I use: locationManager.removeUpdates(locationListener); Its Working

    @Override
protected void onPause() {
    super.onPause();
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        // TODO: Consider calling
        //    ActivityCompat#requestPermissions
        // here to request the missing permissions, and then overriding
        //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
        //                                          int[] grantResults)
        // to handle the case where the user grants the permission. See the documentation
        // for ActivityCompat#requestPermissions for more details.
        return;
    }


    locationManager.removeUpdates(locationListener);
}
Sabari Esh
  • 51
  • 1
  • 1
2

I see it´s been a while since this post, but maybe it would help some body else. I use: removeUpdates(this), because my listener it´s the activity where I implement de location manager, you need to indicate your listener.

Alex
  • 121
  • 3
0

Quite an old queerie but this works for me.

       @Override
        public void onStatusChanged(String s, int i, Bundle bundle) {

        mLocationManager.removeUpdates(locationListener);

        }