[英]How do I make my app wait until AsyncTask has finished its work before moving on to other tasks (Android)
I am doing a geocoding get request using AsyncTask which needs to finish executing, before proceeding to call another AsyncTask which will use the results from the geocoding request to make another call to another endpoint from the MainActivity. 我正在使用AsyncTask进行地理编码获取请求,该请求需要完成执行,然后继续调用另一个AsyncTask,它将使用地理编码请求的结果从MainActivity再次调用另一个端点。
I thought about forcing it to loop in a while using a "done" flag, set by the first AsyncTask, that may not even work however and it seems very hacky. 我考虑过使用由第一个AsyncTask设置的“完成”标志来强制它循环一段时间,但是它可能甚至无法正常工作,而且看起来很hacky。 Does anybody have an idea how to handle this?
有人知道如何处理吗?
AsyncTask
s have the hook method onPostExecute()
, where you can process its results. AsyncTask
具有钩子方法onPostExecute()
,您可以在其中处理其结果。
From there, you can simply trigger any methods you want to be executed after the AsyncTask
is finished. 从那里,您可以简单地在
AsyncTask
完成后触发要执行的任何方法。
For example: 例如:
protected void onPostExecute(Long yourResult) {
processYourResult(yourResult);
// Method startMoreLogic() will be executed after the AsyncTask is finished.
startMoreLogic();
}
make your asynctask as a private class and put everything that wait for result at onPoseExecute(). 将asynctask设为私有类,然后将等待结果的所有内容都放在onPoseExecute()上。
Or add .get() to the method where you call your asynctask. 或将.get()添加到调用asynctask的方法中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.