I have an IntentService which queues up web service calls to be made. I pass an integer as an Extra with each Intent which defines the type of web service call to be made.
I'd like to create a situation where, if an Intent to perform a particular web service is passed to the IntentService and an Intent for that same web service already exists in the IntentService queue, the Intent is not processed. Ideally, I'd throw away the Intent, but I could also add an Extra to it that lets the code know to skip the Intent once it is handled. As Intents come in, I could keep track of which ones are in the queue in some object I add to the IntentService.
However, I don't see a place to intercept the Intent right when it is passed to the IntentService. As far as I can tell, I can only touch it when onHandleIntent is called and that would be too late.
Any ideas?
The Service will only run in one instance.
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.
Limitations / Drawbacks The IntentService cannot run tasks in parallel. Hence all the consecutive intents will go into the message queue for the worker thread and will execute sequentially.
Start a service. An Android component (service, receiver, activity) can trigger the execution of a service via the startService(intent) method. // use this to start and trigger a service Intent i= new Intent(context, MyService. class); // potentially add data to the intent i.
Its not strictly speaking advisable, but I'd suggest something like this:
public int onStartCommand (Intent intent, int flags, int startId)
{
// do your intent modification here
//if(intentIsDuplicate(intent))
// make sure you call the super class to ensure everything performs as expected
return super.onStartCommand(intent, flags, startId);
}
Let me know if that works.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With