Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android How to queue multiple intents on an IntentService

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())

like image 591
ilomambo Avatar asked Feb 12 '13 13:02

ilomambo


People also ask

Can an IntentService execute multiple tasks sequentially?

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.

Is IntentService deprecated?

This class was deprecated in API level 30. IntentService is subject to all the background execution limits imposed with Android 8.0 (API level 26). Consider using WorkManager or JobIntentService , which uses jobs instead of services when running on Android 8.0 or higher.

How to use IntentService in android?

Android App Development for Beginners Intent Service is going to do back ground operation asynchronously. When user call startService() from activity , it doesn't create instance for each request. It going to stop service after done some action in service class or else we need to stop service using stopSelf().

What happens if multiple intents are passed to a started service?

The Service will only run in one instance.


1 Answers

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().

like image 188
Graham Borland Avatar answered Oct 17 '22 10:10

Graham Borland