Mr Asker
Mr Asker

Reputation: 2380

Stop the background service which contains the LocationManager

I am starting my background service from the MainActivity with startService(intent) which sends the location of the user to the server. I want to enable the user to stops this service (also stoping requestLocationUpdates of firing the data from the onLocationChanged method) if he does not wish it by clicking button in the Menu. I have tried the code in the onOptionsItemSelected method but the service it still works when I click button.

How can I stop this service?

MainActivity:

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.route_available);

    Intent i = new Intent(this, TrackingService.class);
    startService(i);

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.disconnect_id:
        System.out.println("test onCreateOptionsMenu was invoked.");

        // Stop the service when the Menu button clicks.
        Intent i = new Intent(this, TrackingService.class);
        stopService(i);

        return true;

    default:
        return super.onOptionsItemSelected(item);
    }
}

Tracking Service class:

public class TrackingService extends Service {

 ....
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // TODO Auto-generated method stub

        Toast.makeText(this, "Location Updation has started", Toast.LENGTH_LONG)
                .show();
        Log.v("X", "Response:in onStartCommand()");

        detectLocation();
        stopSelf();
        return START_STICKY;
    }

    private void detectLocation() {
        // TODO Auto-generated method stub
        Toast.makeText(this, "Inside detectlocation()", Toast.LENGTH_SHORT)
                .show();

        LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        LocationListener ll = new MyLocationListener(this);
        // location updates: at least 0 meter and 60 seconds change.
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 30 * 1000, 0,
                ll);

        Log.v("X", "Response:After lm1.requestLocationUpdates ");
        enableGPS(lm);

    }


}

Upvotes: 1

Views: 138

Answers (1)

Daniel Nugent
Daniel Nugent

Reputation: 43322

It looks like the way you have it coded now, your Service isn't really doing anything.

Because you have registered your MyLocationListener instance as the LocationListener, stopping the Service doesn't do anything.

Also, since you call stopSelf() in onStartCommand(), you're stopping the Service immediately every time it starts.

I would recommend making the Service the LocationListener, and don't call stopSelf() in onStartCommand() . Also, override onDestroy() in the Service, and explicitly call removeUpdates() in order to ensure your app releases it's request to keep the GPS radio active.

Something like this:

public class TrackingService extends Service implements LocationListener{

    LocationManager lm; //make instance variable

    //....
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // TODO Auto-generated method stub

        Toast.makeText(this, "Location Updation has started", Toast.LENGTH_LONG)
                .show();
        Log.v("X", "Response:in onStartCommand()");

        detectLocation();
        //stopSelf(); //don't stop the service here
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        lm.removeUpdates(this);
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }


    private void detectLocation() {
        // TODO Auto-generated method stub
        Toast.makeText(this, "Inside detectlocation()", Toast.LENGTH_SHORT)
                .show();

        lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        //LocationListener ll = new MyLocationListener(this);

        // location updates: at least 0 meter and 60 seconds change.
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 30 * 1000, 0,
                this);

        Log.v("X", "Response:After lm1.requestLocationUpdates ");
        enableGPS(lm);

    }

    @Override
    public void onLocationChanged(Location location) {
        //put your location changed code here

    }

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

    }

    @Override
    public void onProviderEnabled(String provider) {

    }

    @Override
    public void onProviderDisabled(String provider) {

    }

}

Upvotes: 1

Related Questions