Archie.bpgc
Archie.bpgc

Reputation: 24012

Unable to stop LocationListener service

In a Splash Activity's onResume(), I start the service this way

AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent i = new Intent(this, LocationService.class);
PendingIntent pi = PendingIntent.getService(this, 0, i,
            PendingIntent.FLAG_UPDATE_CURRENT);
am.cancel(pi);
am.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                        ystemClock.elapsedRealtime() + 20000, 60000, pi);

and in onStop(), I stop the service this way

stopService(new Intent(this, LocationService.class));

and the Service Class is:

public class LocationService extends Service implements LocationListener {

    private LocationManager mgr;

    @Override
    public IBinder onBind(Intent arg0) {

        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        mgr = (LocationManager) getSystemService(LOCATION_SERVICE);
        //get Location details
        mgr.requestLocationUpdates(LocationManager.GPS_PROVIDER,
                10 * 60 * 1000, 50, this);

        return START_NOT_STICKY;

    }

    private void dumpLocation(Location l) {
    }

    @Override
    public void onLocationChanged(Location location) {
    }

    @Override
    public void onProviderDisabled(String provider) {
    }

    @Override
    public void onProviderEnabled(String provider) {
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
    }

    @Override
    public void onDestroy() {

        super.onDestroy();
        mgr.removeUpdates(this);
    }
}

Once I stop the service in Splash Activity's onStop(), onDestroy() of the LocationService is triggered(which means the service is stopped?), but still after a minute as specified in the AlarmManager the service runs.

Upvotes: 0

Views: 466

Answers (1)

Gabe Sechan
Gabe Sechan

Reputation: 93726

Store the pending intent for the alarm in a variable. Call cancel on it when the service ends.

Upvotes: 1

Related Questions