0

I need to make the IntentService to process only one pending task (the last one). For example if the service is already running and it receive another task then that task is kept in the queue and run after the current one ended. The problem is that if a third task is received then I want that all pending tasks to be removed and only the last one to be left in the queue.

Any ideas how can I do this? So far I was trying to extend Service and create my own custom Service instead of IntentService. I am not sure if this is the right way.

    @Override
public int onStartCommand(Intent intent, int flags, int startId) {
    mServiceHandler.removeCallbacksAndMessages(null); // add this
    onStart(intent, startId);
    return mRedelivery ? START_REDELIVER_INTENT : START_NOT_STICKY;
}
chris
  • 313
  • 2
  • 12
  • 1
    I'm not sure exactly how you want to handle the queued tasks, but [`IntentService`](https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/app/IntentService.java) is a relatively simple `Service` subclass. You could easily modify the source to keep a count or collection of queued `Intent`s, and `removeMessages()` from the `ServiceHandler` as needed. – Mike M. Apr 15 '16 at 01:15
  • The way I see it is that the ServiceHandler instantly send the thread to a Looper and once it gets there there is no way to remove it. Maybe I understand this wrong? – chris Apr 15 '16 at 01:21
  • 1
    `Message`s sent to a `Handler` are queued. Any `Message`s not yet handled can be removed. Otherwise there would be no point to the `removeMessages()` methods. – Mike M. Apr 15 '16 at 01:24
  • So if I make a clone of the class IntentService and add the line removeMessages like I did above you think it should work? – chris Apr 15 '16 at 01:38
  • Um, not really. I read your question as saying that if you had one task running, another queued, and a third was delivered, the second queued task is cancelled. – Mike M. Apr 15 '16 at 01:49
  • What I want is to remove all tasks that are queued when a new one is delivered and that to become the only one in queue. Then when the running task is finished the last task delivered will be processed – chris Apr 15 '16 at 01:52
  • 1
    Oh, OK, so not necessarily based on count. Then yeah, that should work, since the first task will be handled immediately, the second will be queued, and any further will clear the queue before queuing themselves. – Mike M. Apr 15 '16 at 02:00
  • It makes sense. What confused me is that I don't understand how the Looper works and I noticed that there is also a queue of tasks there and I was not sure if I need to access that queue and clear it. – chris Apr 15 '16 at 02:07
  • The `Looper` just takes `Message`s from the queue, and delivers them to its thread. The `MessageQueue` isn't directly exposed in the `IntentService` code. You manage it through interaction with the `ServiceHandler`, and since the only action you need is to clear it, it's going to be pretty simple. – Mike M. Apr 15 '16 at 02:25

0 Answers0