0

I have a foreground service which implements LocationListener. When the Service is killed, it keeps on getting a fix. However this is what I call in the onDestroy()

@Override
public void onDestroy() {
    Log.e(getPackageName(), "Destroying GPSservice");
    super.onDestroy();
    running = false;
    stopForeground(true);
    locationManager.removeUpdates(EventService.this);
    locationManager = null;
}

Is there something I'm missing?

EDIT

More code. This is in the onCreate():

    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    Criteria criteria = new Criteria();
    String bestProvider = locationManager.getBestProvider(criteria, true);

    Location location = locationManager.getLastKnownLocation(bestProvider);
    if (location != null) {
        onLocationChanged(location);
    }
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
            3000L, 15.0f, this);
tolgap
  • 9,629
  • 10
  • 51
  • 65
  • add more info, how your locationListener is implemented? – Simon Dorociak Jun 20 '12 at 15:18
  • I really don't want to sound snobby but: it's implemented like it should be. The GPS itself is working A-OK, but when the Service gets killed, I want the locationManager to remove the updates the Service has. Instead, it keeps on getting a fix. Otherwise, I have to copy past a LOT of code here.. – tolgap Jun 20 '12 at 15:21
  • post some more code to show how your location listener is implemented – Orlymee Jun 20 '12 at 15:32
  • Do you know if your `onDestroy()` method is being called? Do you have clients bound to your service? – David Wasser Jul 05 '12 at 08:41
  • I have a similar problem. In my case the issue has to do with the Activity lifecycle (http://developer.android.com/guide/components/activities.html#Lifecycle). Note how the activity can be killed without calling onDestroy, preventing the location listener from being registered. – mlohbihler Jul 16 '12 at 15:34
  • Sorry, s/b "deregistered". Missed the 5 minute edit window. – mlohbihler Jul 16 '12 at 15:43

2 Answers2

1

use

locationManager.removeUpdates(Your_Location_Listener_instance);

instead of

locationManager.removeUpdates(EventService.this);

for more infromation see doc public void removeUpdates (LocationListener listener)

ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • I have stated above that my LocationListener is implemented by the EventService. So that's my instance I think. – tolgap Jun 20 '12 at 15:17
0

I have solved the problem. It turns out, the MyLocationOverlay binds to the LocationSensorThread too, and I was never turning that off. So that was keeping the GPS awake.

tolgap
  • 9,629
  • 10
  • 51
  • 65