简体   繁体   English

如何让我的应用程式等到AsyncTask完成工作再继续执行其他工作(Android)

[英]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.

相关问题 我的AsyncTask类中还包含其他线程。 在执行以下方法之前,我如何等待它及其所有线程完成? - My AsyncTask class has other threads in it. How can i wait for it and all of its threads to finish before executing a following method? Android AsyncTask等到完成 - Android AsyncTask wait until finished 如何让我的程序等到任务完成后再继续? - How to make my program wait until a task is finished before continuing? 如何使其等到函数返回? 安卓系统 - How do I make it wait until a function has returned? Android Android-如何等待SnapshotListener完成? - Android - How to wait until a SnapshotListener has finished? Android AsyncTask等到Status.FINISHED - Android AsyncTask wait until Status.FINISHED 在我开始我的回收站视图之前,我如何让我的改造等待完成 - How do i get my retrofit to wait until its done before i start my recycler view 如何让IntentService等到BroadcastReceiver完成执行? - How to make an IntentService wait until BroadcastReceiver has finished executing? Android AsyncTask 等待完成以检索数据并将其显示在 ListView - Android AsyncTask wait until finished to retrieve data and display it in the ListView Java / Android等待线程完成后再运行新方法 - Java / Android wait until a thread has finished before running a new method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM