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

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

问题描述

我有一个调用意图服务(DownloadService")的活动(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

intent 服务开始下载数字 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?

简而言之,我需要一种与队列通信的方法来执行这两个简单的操作,但我在互联网上没有发现任何有用的东西:(

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 类,复制非常短的原始类并为我自己的目的修改方法........特别是出队在我的情况下,您可以使用 ServiceHandler 方法的元素
mServiceHandler.removeMessages(appId);
删除消息队列中带有特定代码what"的任何待处理消息帖子,这意味着您必须标记添加到队列中的每条消息,并在每条消息的what"字段内添加一个标识符........例如

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

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

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