Skip to content Skip to sidebar Skip to footer

Drop Intents While Intentservice Is Processing

If I understand the API docs on IntentService correctly, intents sent to an IntentService queue up if it is processing and are processed sequentially. Is there an easy way to tell

Solution 1:

Is there an easy way...?

No, not an easy way that I know of but perhaps not too difficult.

I think I've seen a similar question to this and one suggestion was to simply define your own IntentService class by copying the code from the Android source (there's less than 150 lines of code and some of that is comments).

The 'queue' of Intents is managed by a nested class called ServiceHandler which extends Handler. If you define your own 'IntentService' from the source you could add methods to clear the message queue and even terminate the Handler if it is in the middle of processing an Intent (if that's what you need).

Solution 2:

better late than never. yes, there's an easy way. override onStart(), and return if you don't want to process the intent, or call super.onStart() otherwise.

publicvoidonStart(Intent intent, int startId) {
  if (processing()) {
    return;
  }
  super.onStart(intent, startId);
} 

note however that the javadocs for IntentService specifically say that onStart() should not be overriden.

Post a Comment for "Drop Intents While Intentservice Is Processing"