Android: Intentservice, How Abort Or Skip A Task In The Handleintent Queue
Solution 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
publicvoidclearQueue() {
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 queueclearQueue();
}
super.onStart(intent, startId);
}
Works like a charm :)
Hope it helps someone...
Solution 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
publicvoidonStart(Intent intent, int startId)
{
super.onStart(intent, startId);
Messagemsg= mServiceHandler.obtainMessage();
msg.arg1 = startId;
msg.obj = intent;
msg.what = intent.getIntExtra("appId", 0); \\parameters that come from the outside
Solution 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!
publicclassMyDownloadServiceextendsIntentService {
privatestaticList<String> canceledUrl;
publicstaticvoidcancelDownload(String url){
canceledUrl.add(url);
}
@OverrideprotectedvoidonHandleIntent(Intent intent) {
if (intent != null) {
final String url = intent.getStringExtra(EXTRA_URL);
if(!canceledUrl.contains(url)) //if download has not been canceled alreadydownloadFile(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!
Solution 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
Solution 5:
As said by @JeffreyBlattman above, it is better to play safe by assigning your own "what" value to the message like this
@OverridepublicvoidonStart(@Nullable Intent intent, int startId) {
Messagemsg= 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.
Post a Comment for "Android: Intentservice, How Abort Or Skip A Task In The Handleintent Queue"