donfede
donfede

Reputation: 734

are startService() Intents serialized?

a) if my code calls startService() twice, will the Intents be processeced in the order I called them, or might they get switched randomly?

b) will the first run through onStartCommand() complete before the second call starts, or might they run in parallel?

I have read the android services guide and the reference. While they clearly show how the IntentService worker threads are serialized, I've found little information on how the Intents are delivered and processed.

Upvotes: 0

Views: 194

Answers (1)

CommonsWare
CommonsWare

Reputation: 1007276

if my code calls startService() twice, will the Intents be processeced in the order I called them, or might they get switched randomly?

The behavior for Service in this regard is undocumented, last I checked. However, they should be called in the order they were issued, based on what seems to happen in practice.

will the first run through onStartCommand() complete before the second call starts, or might they run in parallel?

That is up to you. By default, onStartCommand() on a Service is called on the main application thread, and therefore only one command is processed at a time. If you choose to fork threads from onStartCommand() to process commands, those threads may run in parallel.

While they clearly show how the IntentService worker threads are serialized, I've found little information on how the Intents are delivered and processed.

IntentService maintains its own thread. You implement onHandleIntent() instead of (or possibly in addition to) onStartCommand(). Since there is only one thread, only one onHandleIntent() will be executed at a time.

Upvotes: 2

Related Questions