我可以按顺序链接异步任务吗(在上一个异步任务完成后开始一个)
每次我执行 httpRequest
时,在代码执行时屏幕都会被锁定几秒钟。因此,我使用 AsyncTask
在单独的线程中执行所有 httpRequest
操作,同时放置 ProgressDialog
,以便用户知道正在发生某些事情。
我最近遇到了以下情况:我的一个 httpRequest
的输入依赖于前一个 httpRequest
(+parse) 操作的结果。我不能只是按顺序放置两个 AsyncTask,因为 Android 会将它们放入两个线程中,并在第一个线程尚未完成的情况下启动第二个线程。如果没有适当的输入(第一个 httpRequest
的结果),我的第二个 httpRequest
将使应用程序崩溃。
有没有办法可以放入 wait()
来强制第二个 AsyncTask
在第一个任务完成之前不启动?
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.
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 AsyncTask
s 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.
Is there way I can put-in a wait()
to force the second AsyncTask
not to start until the first one finishes?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
前几天我也遇到了同样的情况。
我是这样解决的:
将您的活动的引用传递给异步类的构造函数并在后台函数中执行 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:
Cheers
所以我认为(不确定),您需要在执行后方法上调用第二个 asyncTask
so i think(not sure),you need to call second asyncTask on post execution method
是的,你可以。但它只能在
onProgressUpdate()
或onPostExecute()
中实现。尝试这样做:同样,您必须将
new Task3().execute();
放入 Task2 的AsyncTask< 的
onPostExecute()
方法中/code>不要在主代码中这样调用它们。如果这样做,那么所有这些都将一起运行:
参考: 异步任务
Yes, you can. But it is only possible in
onProgressUpdate()
oronPostExecute()
. Try doing it like this:Similarly, you'll have to put
new Task3().execute();
in theonPostExecute()
method of Task2'sAsyncTask
Don't call them like this in your main code. If you do, then all of them will run together:
Reference: AsyncTask
如何使用
executeOnExecutor
方法:How about using
executOnExecutor
method :偶然发现这个线程正在寻找比我当前使用的更好的解决方案,但它似乎仍然是最好的...
我所做的就是在每个异步任务的构造函数中传递一个可运行的对象。
无论是在执行后还是在后台执行结束时,我都会启动下一个任务(如果我有一个通过执行可运行的任务)。
这样我就可以通过我传递的可运行对象来改变异步任务的顺序,从而保持代码的灵活性,如果我没有传递可运行对象,它们会正常完成。可运行对象仅包含一行调用下一个异步任务。
我只是不喜欢制作所有这些可运行的东西。希望存在像 vb.net 中那样用于链接代表的东西。
Stumbled on this thread looking for a better solution than what I currently use, but it still seems the best...
What I do is pass a runnable in the constructor of each of my async tasks.
Either in the post execute or at the end of the do in the background, I launch my next task, if I have one by executing the runnable.
This way I can changeup the order of my asynctasks by the runnables I pass keeping the code flexible, and if I pass no runnable they complete normally. The runnables just contain one line calling the next asynctask.
I just don't like making all those runnables. Was hoping something existed like in vb.net for chaining delegates.
我以前遇到过这个问题。我所做的是使用回调接口类并在 onPostExecute() 方法内调用其方法。这样你就可以从回调方法中调用另一个AsyncTask。
I encountered this problem before. What I did is use a call back interface class and call its method inside the onPostExecute() method. This way you can call another AsyncTask from the callback method.