How do I stop GPS/location tracking when my Activity finishes?
Are you using MyLocationOverlay? This gives you the pulsating blue dot that represents your current position. I didn't see MyLocationOverlay in your sample code but I'm just double checking.
If you are using MyLocationOverlay...
I've tested the code below: with and without calling "disableMyLocation" when I exit a sample app after pressing an "exit" menu command. When I call "disableMyLocation", the GPS tracking turns off. When I don't call it, the GPS tracking stays on after I call "finish".
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.quit:
myLocOverlay.disableMyLocation(); //turn off location
mlocManager.removeUpdates(this); // no impact on GPS tracking
finish(); //destroy Android app
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Related videos on Youtube
slhck
Video quality guy and researcher, PhD student in computer science. Founder/CEO of AVEQ. I offer personal consulting and help with video encoding, especially with FFmpeg. Send a mail to werner.robitza at gmail.com. More info on my website.
Updated on May 07, 2022Comments
-
slhck almost 2 years
I have a very simple app for Android that displays a Google Maps view and uses the GPS to track the position (essentially like so):
public void onCreate(Bundle savedInstanceState) { // ... mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE); // ... } public void onResume() { super.onResume(); mLocationManager.requestLocationUpdates(mProvider, 20000, 1, this); } public void onPause() { super.onPause(); mLocationManager.removeUpdates(this); } public void onLocationChanged(Location location) { mPosition = getGeoPointForLocation(location); mMapController.setCenter(mPosition); }
And when I use the following command to exit the application (e.g. through a menu), the GPS keeps on tracking - it seems that the Activity is still running:
// ... case R.id.menu_exit: finish(); // ...
How do I stop the GPS tracking if it does not work by removing the location manager in
onPause()
and callingfinish()
? As far as I have read tutorials or other questions, this should be the solution..-
Mathias ConradtThe command removeUpdates is correct. Generally it'd be better to override onFinish() and remove the updates in there. This way you're safer in case the activity gets destroyed in some other way than the exit button. But since you register the locationUpdates in onResume, wouldn't it make even more sense to remove the updates in onPause()?
-
Mathias Conradtbtw - not directly related to your question: for more enhanced functionality later, if you want to have the app working indoor and outdoor and more precise, take a look at this post: stackoverflow.com/questions/3145089/…
-
-
slhck almost 13 yearsThis is really it. I was using the Location Overlay -- thank you very much for your help.
-
CircuitBreaker716 almost 13 yearsNo problem. The code above was just an example: the best place to put the "disableMyLocation" call is in the "OnPause" event. Then call "enableMyPosition" in the "OnResume" event.
-
slhck almost 13 yearsYup, I had it in
onCreate()
but no counterpart for it.