3

I read about the differences between Service and IntentService.

The main thing I read is that IntentService has a Workerhtread that the service runs in.

I need to have a service which run regardless of the application activities/ui, and keeps track of the user location.

The service will implement GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener and also LocationListener, and when running, will update a table with the location of the user all the time (until stopped).

Should I use Service or IntentService?

Ofek Agmon
  • 5,040
  • 14
  • 57
  • 101

1 Answers1

5

An IntentService is meant to be a fire and forget service, for relatively short tasks that may be repeated. Another important distinction is that an IntentService stops itself when onHandleIntent() returns. A regular Service doesn't stop unless you (or the android os) explicitly stop it.It sounds like you are planning on a long-running task that also runs when your app is not in the foreground.

In this scenario you definitely want to use a regular Service. You can still choose to perform the work in a separate thread by creating one inside the service and doing the work in there, but you don't necessarily have to. Remember that by default, a Service runs on the main thead.

Tim
  • 41,901
  • 18
  • 127
  • 145
  • thanks for the well explained answer. I think I'll go with the regular Service and maybe start a thread over there to track the location. One more question - I do have a Fragment with a google map on it, and i do want to inform the Fragment when the location has changed so it can draw the new location. I was thinking about doing this with broadcasting custom intents, and have a receiver listen to them in my fragment. Does that sound OK? – Ofek Agmon Sep 30 '16 at 15:35
  • Yes that sounds ok – Tim Sep 30 '16 at 16:04