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 w开发者_高级运维ay to tell IntentService that it should just drop any intents sent to it while it is processing?
Cheers, Torsten
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).
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.
public void onStart(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.
精彩评论