具有顺序执行的多个AsyncTask [英] Multiple AsyncTasks with sequential execution

查看:183
本文介绍了具有顺序执行的多个AsyncTask的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道之前也曾提出过类似的问题,但这些问题的解决方案对我来说不起作用.情况是,我有一个对象数组,每个对象都通过asynctask发送到上传到服务器.我需要分别发送每个对象,但是列表中的每个对象都使用相同的asynctask,因此在onPostExecute()中调用task.execute并不是一个选项,因为这将导致它无限地调用自身.我已经尝试过类似

I know that Similar questions have been asked before but the solution to those questions will not work for me. The situation is that I have an Array of objects that are each getting sent to uploaded to the server via an asynctask. I need to send each one separately but each object in the list uses the same asynctask so calling task.execute in the onPostExecute() is not an option since that would cause it to infinitely call itself. I have tried something like so

for(each item in list){
    task.execute();
    while(task.getStatus() != android.os.AsyncTask.Status.FINISHED){
            //spin here and wait
    }
}

但是,这样做只会永远旋转,并且永远不会离开while循环. 任何建议都将不胜感激

But when doing this it just spins for ever and never leaves the while loop. Any suggestions are greatly appreciated

推荐答案

来自AsyncTask文档:

From AsyncTask docs:

从HONEYCOMB开始,任务在单个线程上执行,以避免并行执行引起的常见应用程序错误.

Starting with HONEYCOMB, tasks are executed on a single thread to avoid common application errors caused by parallel execution.

因此,如果您不是> = HONEYCOMB,则可以切换到使用ExecutorService:

so if you are not >= HONEYCOMB, then maybe switch to use ExecutorService:

ExecutorService executor = Executors.newSingleThreadExecutor();

这篇关于具有顺序执行的多个AsyncTask的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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