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

社会主义新天地 提交于 2019-12-18 12:17:33

问题


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


回答1:


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...




回答2:


I create my own MyIntentService class copying the original one that is pretty short and modifying methods for my own purpose........in particular to dequeue an element you can use methods of ServiceHandler in my case
mServiceHandler.removeMessages(appId);
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



回答3:


Extend the IntentService class and declare a list for cancelled item on it and whenever you want to cancel something add it to this list. finally before handling your intent make sure it has not been cancelled!

public class MyDownloadService extends IntentService {
    private static List<String> canceledUrl;

    public static void cancelDownload(String url){
         canceledUrl.add(url);
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        if (intent != null) {
            final String url = intent.getStringExtra(EXTRA_URL);

            if(!canceledUrl.contains(url)) //if download has not been canceled already
                downloadFile(url);
            else
                canceledUrl.remove(url);
        }
    }
}

I know this code works because i tested it before, But i'm not sure it's a right way to do it!




回答4:


You can bind to the intentservice and create a method to cancel or de queu a download.

Here is a quick tutorial what you might need




回答5:


As said by @JeffreyBlattman above, it is better to play safe by assigning your own "what" value to the message like this

@Override
    public void onStart(@Nullable Intent intent, int startId) {
        Message msg = mServiceHandler.obtainMessage();
        msg.arg1 = startId;
        msg.obj = intent;
        msg.what = 0;
        mServiceHandler.sendMessage(msg);
    }

and clear the queue like mServiceHandler.removeMessages(0).

Hope that helps.



来源:https://stackoverflow.com/questions/7318666/android-intentservice-how-abort-or-skip-a-task-in-the-handleintent-queue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!