[英]Java Async wait for finish
Ho do i get my function to wait for being done with it's async task before returning the variable? 我如何在返回变量之前让函数等待异步任务完成?
public boolean CheckOnline(){
OnlineAsyncTask onlinetsk = new OnlineAsyncTask();
onlinetsk.execute();
return Online;
}
You can call get()
to wait for the async task to complete and retrieve the result. 您可以调用get()
等待异步任务完成并检索结果。
However that defeats the purpose of an async task - it's no longer asynchronous. 但是,这违背了异步任务的目的-它不再是异步的。 Consider redesigning your app so that you don't need to wait for a result. 考虑重新设计您的应用程序,这样您就不必等待结果。 Instead eg use a callback interface to notify that the async task has finished and a result is available. 相反,例如,使用回调接口来通知异步任务已完成且结果可用。
String str_result= new OnlineAsyncTask().execute().get();
这将使其等待直到返回值
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.