15

I am a little bit confused regarding the usage of IntentService.

  1. The documentation says that IntentService queues all intents sent to it and process them one at a time.
  2. I took a look at the code of IntentService and I saw that onStartCommand() receives the intent, calls onStart() which sends it as a message to the intents queue

I am pretty sure I read somwhere in the documentation that onStartCommand() is called by the system only once, if you issue twice a startService(), the second call will not result in onStartCommand() being called.
I might be wrong here, because I have been looking for this piece of documentation and I cannot seem to find it.
This contradicts the previous concept that says you can queue many intents in IntentService through onStartCommand().

So I need help here, how do I queue multiple intents on an IntentService?

I see only two options:

  • Just call everytime startService() with different intents

  • Call directly onStart() or onStartCommand() (bypassing startService())

ilomambo
  • 8,290
  • 12
  • 57
  • 106

1 Answers1

13

You send the Intent with Context.startService() and the Intent is picked up by your service in onHandleIntent().

The first time you call startService() will result in the service's onStartCommand() being invoked. Think of it as a constructor. Subsequent calls to startService() do not need to start the service again, since it's already running; they will just result in more calls to the service's onHandleIntent().

Graham Borland
  • 60,055
  • 21
  • 138
  • 179
  • 1
    According to the code of IntentService (see the link in my question) you will see that it is impossible to call directly onHandleIntent(), only the message Handler can do that. The system has to call either onStart() ot onStartCommand(). – ilomambo Feb 12 '13 at 13:34
  • 2
    Yes, that is correct. All you need to do is call `startService()`, and implement `onHandleIntent()` inside the service. The system does everything else for you, including deciding how and when to actually start the service. – Graham Borland Feb 12 '13 at 13:56
  • Sidenote: Does this mean that I can intercept the intent queueing by overriding *onStart()*? (I know it sounds bad, since *onStart()* was deprecated as API, but in principle?) – ilomambo Feb 12 '13 at 14:02
  • 1
    @ilomambo i think the onStartCommand runs everytime the startService is called(just debug the code and you will know :)) I just want to kno whether the intents are stored by it or not, because i got some null pointer exceptions when i queue some intent services – berserk Dec 05 '13 at 09:17
  • @berserk i was facing the same problem of getting NullPointerException for next intent service call, i simply removed onStartCommand overriden method from my IntentService and it is resolved. I checked the code of IntentService class mentioned above in ilomambo question, and found hint in method comment of onStartCommand that you should not override this method. Not sure bt you can try. – Ankit Dec 09 '13 at 07:47
  • @Ankit I see. Actually I am overriding the onStartCommand to get the size of services queued in it. I am incrementing a variable inside it. Is there any way to get the queue size? – berserk Dec 09 '13 at 09:16