安卓:intentservice,如何终止或跳过任务在handleintent队列 [英] Android: intentservice, how abort or skip a task in the handleintent queue

查看:905
本文介绍了安卓:intentservice,如何终止或跳过任务在handleintent队列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个活动(ApplicationActivity)调用的意图服务(下载服务)

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

这是互联网的背景intentService下载文件,但我希望能够中止特定下载.........

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

所以我们可以说,我把5个文件中的队列:文件1,2,3,4,5

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

这样做的目的服务开始下载数字1,然后是第二等.... 1)有没有办法说的意图服务中止你此刻的方法处理事件在做什么(在这种情况下,下载文件1),并开始下载下一个?

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)是否有可能从队列中删除元素,因此,例如当正在下载文件1,从队列中删除该文件4,使3号后,径直到5?

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?

不久,我需要一种方法的comunicate与队列来执行这两个简单的操作,但我没有找到什么有用的互联网:(

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

推荐答案

在我创建自己的MyIntentService类复制原来的一个是pretty的短期和修改为我自己的目的的方法........特别是出列,您可以使用ServiceHandler的方法在我的案例 - 一个元素 mServiceHandler.removeMessages(APPID);
即删除邮件的任何未决的职位有一定的code是什么是在消息队列中,这意味着你必须标记每个你在队列中添加每个消息的什么字段中的标识符添加消息.....例如:

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

这篇关于安卓:intentservice,如何终止或跳过任务在handleintent队列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆