14

I'm a little confused about intentService. The docs say that if you send an intentService multiple tasks (intents) then it will execute them one after the other on one separate thread. My question is - is it possible to have multiple intentService threads at the same time or not? How do you differentiate in the code between creating three different intents on the same intentService (the same thread), or three separate intentServices each with it's own thread and one intent each to execute?

In other words, when you perform the command startService(intent) are you putting the intent in a single queue or does it start a new queue every time?

Intent someIntent1 = new Intent(this, myIntentService.class);
Intent someIntent2 = new Intent(this, myIntentService.class);
Intent someIntent3 = new Intent(this, myIntentService.class);
startService(someIntent1);
startService(someIntent2);
startService(someIntent3);

1 Answer 1

12

1) Is it possible to have multiple intentService threads at the same time or not?

No, each IntentService only has one HandlerThread that it uses to execute requests in the order that "startService" is called. Unless, for some reason you decide to spawn your own Thread/Threads in the IntentService, but that would likely defeat the purpose of using IntentService in the first place. Services of the same manifest declaration i.e. service name=".MyIntentService" (and this is the same for normal Services) run as a singleton within their process, so until the Service is killed the same Service will receive additional start requests.

2) How do you differentiate in the code between creating three different intents on the same IntentService?

To differentiate between requests, use the Intent system as it's intended! Provide different "Actions" for different jobs the service can carry out, and pass along any extras the IntentService needs to run correctly for that particular job as extras in the Intent object you're using to start the Service.

17
  • 4
    Yes, all requests to startService will hit onHandleIntent() in the order in which startService is called with each intent object. So, basically you're queueing up three "jobs" for the Intent service to run in a queue fashion. Once onHandleIntent() finishes running/or being blocked by the current operation, it'll be hit by the next startService(Intent intent) request that you've queued up. Once all "startService" jobs are finished, the intent service will shut itself down, because it's underlying Handler doesn't have anymore messages to "handle".
    – Submersed
    Jun 13, 2016 at 17:14
  • 2
    @Jon call startService(someIntent1) twice, call startService(someIntent2) four times and add some Log.d inside each onHandleIntent method and you will see how it works
    – pskink
    Jun 13, 2016 at 17:21
  • 1
    @Submersed: "how?" -- by creating more than one subclass of IntentService. "The first, if already created in the current App process will receive additional messages" -- only for startService() calls with an Intent identifying that IntentService. Each IntentService has its own HandlerThread, which in turn has its own MessageQueue. While an individual Service class is effectively a singleton, separate Service classes have independent lifecycles, just as separate Activity instances do. And they can all be in one process. Jun 13, 2016 at 17:48
  • 1
    @Jon: "do you mean that it's possible to have multiple intentServices in the same process" -- yes, if they are separate implementations. In your question, you appear to be starting the same service (myIntentService) three times. That will create one instance of the service, which will process the three queued commands sequentially. If, OTOH, you have three separate IntentService subclasses (e.g., myIntentService, myIntentService2, myIntentService3), and you call startService() once on each of those, they will run in parallel. Jun 13, 2016 at 17:50
  • 1
    @CommonsWare Yea, me too. I simplified further, since that portion wasn't really necessary to get the point across, just the fact that Services are singletons within their process -- which is really all I was trying to say there. I'm interested in testing out a few use cases now though :)
    – Submersed
    Jun 13, 2016 at 18:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.