user280560 user280560 - 1 year ago 120
Android Question

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

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 :(


Answer Source

I create my own MyIntentService class copying the original one that is pretty short and modifying methods for my own particular to dequeue an element you can use methods of ServiceHandler in my case
that Remove any pending posts of messages with a certain code 'what' that are in the message queue, this means that you have to label each message you add in the queue adding an identifier inside the "what" field of each message.....for example

public void onStart(Intent intent, int startId) 
    super.onStart(intent, startId);
    Message msg = mServiceHandler.obtainMessage();
    msg.arg1 = startId;
    msg.obj = intent;
    msg.what = intent.getIntExtra("appId", 0); \\parameters that come from the outside