我可以按顺序链接异步任务吗(在前一个异步任务完成后开始一个) [英] Can I chain async task sequentially (starting one after the previous asynctask completes)

查看:34
本文介绍了我可以按顺序链接异步任务吗(在前一个异步任务完成后开始一个)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每次我执行 httpRequest 时,在代码执行时屏幕会出现几秒钟的锁定状态.因此,我使用 AsyncTask 在一个单独的线程中执行我所有的 httpRequest 内容,同时放置一个 ProgressDialog 以便用户知道正在发生的事情.

Every time I do a httpRequest, the screen will appear to be locked up for a few seconds while the code is executing. Hence I used AsyncTask to do all my httpRequest stuff in a separate thread while putting up a ProgressDialog so the user knows something is happening.

我最近遇到了以下情况:我的一个 httpRequest 的输入依赖于前一个 httpRequest (+parse) 操作的结果.我不能只是将两个 AsyncTask 依次放置,因为 Android 会将它们放在两个线程中并在第一个线程未完成的情况下启动第二个线程.如果没有适当的输入(第一个 httpRequest 的结果),我的第二个 httpRequest 将使应用程序崩溃.

I recently encountered the following situation: the input of one of my httpRequest is dependent on the result from a previous httpRequest (+parse) action. I can't just put the two AsyncTasks sequentially cause Android will put them in two threads and start the second one without the first one being finished. And without an appropriate input (the result of the first httpRequest), my second httpRequest will crash the app.

有没有办法我可以放入一个 wait() 来强制第二个 AsyncTask 在第一个完成之前不启动?

Is there way I can put-in a wait() to force the second AsyncTask not to start until the first one finishes?

推荐答案

前几天我也遇到了同样的情况.我是这样解决的:将您的活动的引用传递给异步类的构造函数并在后台函数中执行 do.现在,在执行后函数中,从异步类调用您活动的公共方法以再次执行任务...或尝试以下操作:

I also had some same situation the other day. I had solved it in this way: Pass the reference of your activity to the constructor of async class and execute the do in background function. Now in post execute function call a public method of ur activity from async class to execute the task again... or try this:

            if (asynclass.getStatus() == android.os.AsyncTask.Status.PENDING) {
                asynclass.execute();
            } else if (RF.getStatus() == android.os.AsyncTask.Status.FINISHED) {
                asynclass = new asyncclass();
                asynclass.execute();
            } else {
                Toast.maketoast(this, "Plz wait", 1).show();
            }

干杯

这篇关于我可以按顺序链接异步任务吗(在前一个异步任务完成后开始一个)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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