多个AsyncTasks的串行执行
有谁知道如何轻松地安排对队列或其他内容中的 AsyncTask 执行的多个调用,然后以串行方式执行它们?
我希望被调用的异步任务在完成之前等待,但似乎我无法完成此任务,即使我测试当前正在执行的任务的状态也是如此。
有什么想法如何解决这个问题吗?我已经看到在 honeycomb API 中有一个带有 SERIAL_EXECUTOR
的方法 executeOnExecutor()
,我猜它实现了我所描述的内容。但是,我不是为蜂窝开发。
谢谢!
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.
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.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你可以尝试一下IntentService。来自参考:
我从广播接收器运行该服务,如下所示:
它按照广告中的方式工作。请求序列化得很好。该服务在收到请求时启动。处理完最后一个请求后,服务停止。请求按照 FIFO 进行处理。
我通过右键单击源包名称并选择“新建/类”来创建意图服务。我使用intentservice作为超类。构造函数有一个“名称”参数。我将其更改为:
该服务的所有代码都进入了 onHandleIntent 函数。我不必使用任何其他 @Override 函数。
希望这就是您想要的...
注意:变量“context”是 onReceive 中传递的参数。我将代码中的名称从 XxxxService 更改为“Service”或“service”。最后,我在所有项目中创建一个名为 GC 的类。它是全局常量的容器类。 GC.EXTRA_SERVICE_DATA 是定义额外键的全局字符串。
You might try IntentService. From the reference:
I ran the service from a broadcast receiver as follows:
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.
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:
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...
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.
Honeycomb以上的Android版本已经将Asynctask的默认执行器设置为串行执行器。所以如果你的分钟。 SDK版本和目标SDK版本大于12,Asynctasks将按照其execute函数的调用执行。请参阅我对 Asynctask 的讨论:
希望这对您有帮助...
Android versions greater than Honeycomb have made the default executor of Asynctask as serial executor. So if your min. SDK version and the target SDK version are more than 12, Asynctasks will execute in accordance to the calling of their execute function. See my discussion on Asynctask at
Hope this helps you...
我会这样做,有一个数组或队列或计数器,用于记录您想要在活动中执行 AsyncTAsk(AsyncTask 的同一类或不同类)的次数,在 AsyncTask 中,onPostExecute(),递减计数器(或类似的)数据结构)并再次调用 AsyncTask。
I would do it this way, Have an array or a queue or counter of number of times you want to execute AsyncTAsk(same class of AsyncTask or different class) in an activity, In AsyncTask, onPostExecute(), decrement the counter(or similar data structure) and call the AsyncTask again.