Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tnx

like image 352
Sgotenks Avatar asked Sep 06 '11 10:09

Sgotenks


People also ask

Why IntentService is deprecated?

Provided since Android API 3, the purpose of IntentService was to allow asynchronous tasks to be performed without blocking the main thread. The deprecation is one of a number of steps introduced starting with Android 8.0 (API 26) to limit the actions that can be performed while an app is in the background.

Does IntentService Use main thread?

If the task doesn't require any and also not a very long task you can use service. If the Background task is to be performed for a long time we can use the intent service. Service will always run on the main thread. intent service always runs on the worker thread triggered from the main thread.

How do I start IntentService?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. In the above code, we have taken text view, when user gets the data from intent service it will update.


2 Answers

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

like image 68
Joao Sousa Avatar answered Oct 22 '22 22:10

Joao Sousa


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
like image 36
Sgotenks Avatar answered Oct 22 '22 20:10

Sgotenks