Cannot stop LOCATION_SERVICE
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 LocationManag开发者_开发问答er 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.
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.
I think you should remove the @Override
.
I've tested this and got the same problem. And with @Override
my application crashes.
I also think, that you're using the wrong method. onDestroy is the last one you should execute. Just have a look at the activity documentation there is a diagram showing the involved methods for an application.
link text
I'm trying to do the same as you and I've used onResume and onPause to activate and deactivate the LocationUpdates.
精彩评论