Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capturing IntentService Intents before onHandleIntent

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?

like image 532
Andrew Avatar asked Sep 11 '11 23:09

Andrew


People also ask

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

The Service will only run in one instance.

Why 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.

What are the limitations of the IntentService?

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.

What are the steps to create an intent Service?

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.


1 Answers

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.

like image 102
Femi Avatar answered Sep 30 '22 06:09

Femi