多AsyncTasks的串行执行 [英] Serial Execution of multiple AsyncTasks
问题描述
有谁知道如何轻松地安排多次调用一个AsyncTask的执行在队列或东西,然后以串行方式执行呢?
Does anyone know how to easily arrange multiple calls to an AsyncTask execution in a queue or something and then execute them in a serial fashion?
我要叫异步任务等待完成之前,却是一个看起来我不能做到这一点,即使我测试目前正在执行的之一的地位。
I want the called async task to wait while the one before it is finished, but is seems I can't accomplish this, even if I test the status of the one currently being executed.
任何想法如何解决这个问题?我见过的蜂窝API中有一个方法 executeOnExecutor()
与 SERIAL_EXECUTOR
,我想它实现我所描述的。但是,我不开发蜂窝。
Any ideas how to solve this? I've seen that in the honeycomb API there is a method executeOnExecutor()
with a SERIAL_EXECUTOR
, I guess it implements what I've described. However, I'm not developing for honeycomb.
谢谢!
推荐答案
您可以尝试IntentService。从参考:
You might try IntentService. From the reference:
IntentService对于那些处理需求的异步请求(pssed的意图EX $ P $)服务的基类。客户端通过startService(意向)发送呼叫请求;根据需要该服务已启动,把手又用一个工作线程的每个意图,并停止时,它本身耗尽的工作。
IntentService is a base class for Services that handle asynchronous requests (expressed as Intents) on demand. Clients send requests through startService(Intent) calls; the service is started as needed, handles each Intent in turn using a worker thread, and stops itself when it runs out of work.
我跑从广播接收器的服务如下:
I ran the service from a broadcast receiver as follows:
// Now let the service module do the rest
Intent serviceIntent = new Intent(context, Service.class);
serviceIntent.putExtra(GC.EXTRA_SERVICE_DATA, serviceData);
ComponentName compName = context.startService(serviceIntent)
它的工作作为标榜。该请求是序列化的罚款。该服务开始收到一个请求时。处理的最后一个请求后该服务停止。这些请求进行处理FIFO。
It worked as advertised. The requests are serialized fine. The service started when a request was received. The service stopped after the last request processed. The requests were processed FIFO.
我使用的源代码包的名称右击并选择新建/类创建的intentservice。我用intentservice作为超。构造函数有'名'的实际参数的。我把它改为:
I created the intentservice using right click on the source package name and selecting New/Class. I used intentservice as the superclass. The constructor has an arguement of 'name'. I changed it to:
public XxxService() {super("XxxService");}
所有的code的服务走进onHandleIntent功能。我没有使用任何其他@Override功能。
All of the code for the service went into the onHandleIntent function. I didn't have to use any other @Override functions.
希望这是你想要的......
Hope this is what you wanted...
注:变量语境是的onReceive传递的参数。我在code从XxxxService到服务或服务改变了名字。最后,我创建一个类,在我所有的项目叫GC。这是一个容器类全局常量。 GC.EXTRA_SERVICE_DATA是一个全球性的字符串定义额外的按键。
Notes: The variable 'context' is a passed parameter in onReceive. I changed the names in the code from XxxxService to 'Service' or 'service'. Finally, I create a class in all my projects called GC. It is a container class for global constants. GC.EXTRA_SERVICE_DATA is a global String defining the extra key.
这篇关于多AsyncTasks的串行执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!