6

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?

Andrew
  • 20,756
  • 32
  • 99
  • 177

1 Answers1

5

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.

Femi
  • 64,273
  • 8
  • 118
  • 148
  • I saw this method on the IntentService documentation, but I wasn't quite sure when it is called. I also noticed that it is not to be overridden. Any idea why? – Andrew Sep 11 '11 at 23:41
  • Basically the internal `onStartCommand` call probably handles the queueing: if you override it and make sure you actually call the superclass's method you SHOULD be fine. – Femi Sep 12 '11 at 01:03
  • This appears to be working fine. I keep a map where the Key is an Integer representing the type of web service being called and the Value is a Boolean representing whether or not an Intent for that call exists in the queue. In onStartCommand I see if the incoming Intent is for a call that already exists in the queue. If it does, I add an extra called "skip" to it. In onHandleIntent, I check for "skip" and skip any code if it exists. But before leaving onHandleIntent I update the map and set the Value for that Key to false. – Andrew Sep 12 '11 at 02:24
  • Additionally, you should probably only do the skip check if flags == 0. Also, it may be a good idea to wrap your onHandleIntent code in a try/catch in case of an exception. Not doing this may leave your flag for that type of web service "stuck" on. – Andrew Sep 12 '11 at 02:33
  • 1
    It is essential that the code you write in your overridden onStartCommand not prevent the super method from being called. For this reason, I've wrapped my code in a try/catch. I had an exception in my code that was preventing the super method from being called. Subsequent retries automatically executed by Android then caused the app to crash because calling the super method will retry flags does not work well when the Intent never originally reached the super method. – Andrew Sep 12 '11 at 18:44