java 我可以按顺序链接异步任务吗(在前一个异步任务完成后开始一个)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7494515/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can I chain async task sequentially (starting one after the previous asynctask completes)
提问by adjfac
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.
每次我执行 a 时httpRequest
,在代码执行时,屏幕似乎会被锁定几秒钟。因此,我过去常常在一个单独的线程中AsyncTask
做我所有的httpRequest
事情,同时放置一个ProgressDialog
以便用户知道正在发生的事情。
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.
我最近遇到了以下情况:其中一个 my 的输入httpRequest
依赖于先前httpRequest
(+解析)操作的结果。我不能只是将两个AsyncTask
s 顺序放置,因为 Android 会将它们放在两个线程中并在第一个线程未完成的情况下启动第二个线程。如果没有适当的输入(第一个的结果httpRequest
),我的第二个httpRequest
将使应用程序崩溃。
Is there way I can put-in a wait()
to force the second AsyncTask
not to start until the first one finishes?
有没有办法我可以输入 await()
来强制第二个AsyncTask
在第一个完成之前不开始?
采纳答案by Awais Tariq
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:
前几天我也遇到了同样的情况。我以这种方式解决了它:将您的活动的引用传递给异步类的构造函数并在后台函数中执行 do。现在,在执行后函数中,从异步类调用您活动的公共方法以再次执行任务……或尝试以下操作:
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();
}
Cheers
干杯
回答by Nikunj Patel
so i think(not sure),you need to call second asyncTask on post execution method
所以我认为(不确定),您需要在执行后方法上调用第二个 asyncTask
protected void onPostExecute(final Void unused) {
if (this.dialog.isShowing()) {
this.dialog.dismiss();
new secTask().execute();
}}
回答by Sheharyar
Yes, you can. But it is only possible in onProgressUpdate()
or onPostExecute()
. Try doing it like this:
是的你可以。但它只能在onProgressUpdate()
或 中onPostExecute()
。尝试这样做:
private class Task1 extends AsyncTask<Void, Void, Void> {
...
...
protected void onPostExecute(Void unused) {
dialog1.dismiss();
new Task2().execute();
}
}
Similarly, you'll have to put new Task3().execute();
in the onPostExecute()
method of Task2's AsyncTask
同样的,你得把new Task3().execute();
在onPostExecute()
任务2的的方法AsyncTask
Don't call them like this in your main code. If you do, then all of them will run together:
不要在主代码中这样称呼它们。如果你这样做,那么所有这些都会一起运行:
// This is wrong
new Task1().execute();
new Task2().execute();
new Task3().execute();
Reference: AsyncTask
参考:AsyncTask
回答by Mickey Tin
How about using executOnExecutor
method :
使用executOnExecutor
方法如何:
public void runTasks() {
AsyncTask<Void,Void,Void> aTask = myTask();
AsyncTask<Void,Void,Integer> aTask1 = myTask1();
AsyncTask<Void,Void,Void> aTask2 = myTask2();
aTask.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR);
aTask1.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR);
aTask2.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR);
}
回答by sean262 none
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.
我所做的是在我的每个异步任务的构造函数中传递一个 runnable。
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.
无论是在执行后还是在后台执行结束时,我都会启动我的下一个任务,如果我通过执行 runnable 有一个任务。
if(doOnPostExec != null)
doOnPostExec.run();
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.
通过这种方式,我可以通过传递的可运行对象来更改异步任务的顺序,以保持代码的灵活性,如果我不传递可运行对象,它们将正常完成。runnables 只包含一行调用下一个异步任务。
I just don't like making all those runnables. Was hoping something existed like in vb.net for chaining delegates.
我只是不喜欢制作所有这些可运行的东西。希望在 vb.net 中存在一些用于链接代表的东西。
回答by neilQ5
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.
我以前遇到过这个问题。我所做的是使用回调接口类并在 onPostExecute() 方法中调用其方法。通过这种方式,您可以从回调方法调用另一个 AsyncTask。