tags:

views:

33

answers:

1

I have a simple application that uses the Android system service LOCATION_SERVICE. On app close I need the service to stop. I have searched hard and long for how to do this but I must not be looking for the right thing. Does anybody know how to stop this service?

Here is what I'm doing.

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main); 

    private LocationManager lm;
    private LocationListener locationListener;

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

    locationListener = new MyLocationListener();

    lm.requestLocationUpdates(
        LocationManager.GPS_PROVIDER, 
        0, 
        0, 
        locationListener);

    ...
}

@Override
protected void onDestroy() {
    lm.removeUpdates(locationListener);
    super.onDestroy();
}

The LocationManager class does not seem to have any methods to stop the service. That's where I thought you would be able to close or stop it.

Thanks for any help.

A: 

You have to call finish() in the onBackPressed() function to close your application. Then the onDestroy should be called to.

There is no need to stop the location service (guess you're talking about GPS). GPS won't be used if there is no application listening to it, hence not consume any CPU power or battery.

Tseng
Yeah I've tried all that. Pretty much it looks like the application is still running even after I back out from the app. Do you know how I can make sure that the application actually closes when I back out?
jonsinfinity