Update
All the answers I got say "because the service runs in the background".
This does not answer my question.
Let's say I call locationManager.requestLocationUpdates
from my activity and after a while my activity is destroyed.
How does locationManager
know that my activity is destroyed and it should not call the listener anymore?
In what way does registering from a service different than registering from an activity?
The original question
My Android app needs to get location updates even when it is in the background.
The canonical answer in SO is usually to create a service for that.
How does a service help?
To create the LocationManager
and register for updates, I use the app's context, e.g.:
mLocationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
mlocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 4000, 0, listener);
So nothing really "binds" the location updates to the service.
What is the difference between registering for location updates from an activity as opposed to registering from a service?