Android: intentservice, how abort or skip a task in the handleintent queue

Sgotenks picture Sgotenks · Sep 6, 2011 · Viewed 8.8k times · Source

i have an activity ("ApplicationActivity") that call an intent service ("DownloadService")

The intentService download files from internet in background, but i want to be able to abort a specific download.........

So let's say that i put 5 files in queue: file 1,2,3,4,5

The intent service start downloading the number 1, then the second and so on.... 1) Is there a way to say to the intent service abort what you are doing at the moment in the method handle event (in this case downloading file 1) and start downloading the next one?

2)Is it possible to remove element from the queue, so for example while is downloading file 1, remove the file 4 from the queue so that after the number 3 it goes straight to the 5?

Shortly, i need a way to comunicate with the queue to perform these 2 simple operations, but i didn't find nothing usefull on internet :(

Tnx

Answer

Joao Sousa picture Joao Sousa · May 23, 2012

Thanks @user280560, I found a solution based on your comment :)

just to give a more specific example, where I wanted to clear the queue in certain cases.

First I copied the IntentService.java source from here to my project (no need to change names, you can keep IntentService.java, just import yours and not android's). Then I added this

public void clearQueue() {
    Debug.PrintInfo(TAG, "All requests removed from queue");
    mServiceHandler.removeMessages(0);
}

to my IntentService source.

Now, from my service that extends IntentService, I wanted to clear the queue when a certain action (login) was passed to the service, so I override the onStartMethod, like this:

@Override
public void onStart(Intent intent, int startId) {
    if(intent.getAction().equals(ACTION_LOGIN)) {
        //Login clears messages in the queue
        clearQueue();
    }
    super.onStart(intent, startId);
}

Works like a charm :)

Hope it helps someone...