-1

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?

daramasala
  • 3,040
  • 2
  • 26
  • 33

3 Answers3

1

Because Service stays running in background all the time. On the other hand, Activity is just an UI, which depend on user actions (screen rotation, minimazing app, etc (see Activity lifecycle)). You can also bind to Service and call methods ason ordinary object (after binding). If you bind and start Service, you can be sure it will now miss any data

Alex Shutov
  • 3,217
  • 2
  • 13
  • 11
0

Service runs in the background, usually also in different thread. If your activity is moved background it is destroyed, you would not get locations updates. The service will be running in the background, and you will be informed of updates.

R. Zagórski
  • 20,020
  • 5
  • 65
  • 90
  • 1
    you have to add concurrency explicitly, thought, IntentService is running on new thread by default, but just once – Alex Shutov Jun 27 '16 at 14:50
0

The point is that you setup the location listener and request for updates in the service. Doing this, the listener is ALWAYS running. Then, when you open your activity you can bind the service and read the locations that the service has stored.

I think that you are doing all the stuff in the activity and that's why you are not undestanding the difference.

adalpari
  • 3,052
  • 20
  • 39
  • How does the location manager know I set it up from a service if it uses the application's context? – daramasala Jun 29 '16 at 09:30
  • It doesn't matter. The location manager does not have to know that. The difference is thath the service is always running util you stop it. The activity stops (and also stop location management) when you close it or open another activity. – adalpari Jun 29 '16 at 09:45
  • How does the activity stop the location management? That's my question. What is the association between the activity and the location manager. – daramasala Jun 29 '16 at 09:48
  • The activity is just no loger running when you stop it. If it's not running, there's no possibility to handle locationMnager callbacks. If you mean "how to stop", you have to remove listener in onStop method: locationManager.removeUpdates(locationListener); – adalpari Jun 29 '16 at 09:51
  • Why there's no possiblity to handle callbacks? If I register a listener that writes the location to user default, why can't it work even after the activity was destroyed? – daramasala Jun 29 '16 at 09:58
  • You said it: because the activity is destroyed (or even stopped). It's NOT running. How are you gonne wite the location into a TextView if the activity is not running??? – adalpari Jun 29 '16 at 10:23
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/115945/discussion-between-daramasala-and-adalpari). – daramasala Jun 29 '16 at 10:30