Reputation: 2229
Suppose I want my app to sync data from an online database to the local database, I understand Services are the components to look for.
If I start a normal service, I understand that it starts in the main thread and any time-consuming task I run will block the UI. This means that I will have to use something like an AsyncTask to get the job done.
On the other hand, if I use an Intent Service, I understand that it runs on a parallel thread and the UI isn't blocked if I run a long task. And any other intent service I call will be queued up.
But what will happen if I create a parallel thread from the IntentService to make a network call?
Will the intent service wait for my network call to complete or will it make the call in a parallel thread and execute the next Intent Service in the queue?
Upvotes: 2
Views: 2037
Reputation: 95578
Only the lifecycle calls (eg: onCreate()
, onStartCommand()
) are called on the main (UI) thread in a Service
. If you have background actitivies, you can just start your own background threads in onStartCommand()
. You don't need to use IntentService
for this.
IntentService
is a specific implementation of Service
that has a specific behaviour. You send it "work" by calling startService()
and passing an Intent
that describes the work to be done. The IntentService
runs until all of the queued "work" is completed and then shuts itself down. IntentService
is just a thi8n layer on top of regular Service
that handles the queueing of the "work", the execution of the "work" in background threads, and the automatic shutdown of the IntentService
. If this is not exactly the behaviour that you want, you shouldn't try to to use IntentService
, as it isn't appropriate for all situations. It isn't that difficult to start background threads in a Service
yourself.
Upvotes: 3