等到异步任务完成其工作 [英] Waiting till the async task finish its work

查看:91
本文介绍了等到异步任务完成其工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很新的节目,我有一些疑问。

我有一个的AsyncTask 这就是我所说的 RunInBackGround

和我开始这个过程中,如:

 新RunInBackGround()执行()。
 

不过,我想等到这个调用完成其执行,继续为code中的其他语句之前。

我怎么能这样做?

还有什么办法呢?

解决方案
  

等待,直到这一呼吁是完成其执行

ü将需要调用<一个href="http://developer.android.com/reference/android/os/AsyncTask.html#get%28%29">AsyncTask.get()方法得到结果返回,并等待,直到doInBackground执行不彻底。但是的这将冻结主UI线程,如果你不打电话获得一个线程里面的方法的。拿到结果早在UI线程启动的AsyncTask为:

 字符串str_result =新RunInBackGround()执行()得到()。
 

I'm very new to programming and I have some doubts.

I have a AsyncTask which is I call as RunInBackGround.

and I start this process like:

new RunInBackGround().execute();

But I wish to wait until this call is finish its executing, before proceeding to the other statements of code.

How can I do that?

Are there any way for it?

解决方案

wait until this call is finish its executing

u will need to call AsyncTask.get() method for getting result back and make wait until doInBackground execution is not complete. but this will freeze Main UI thread if you not call get method inside a Thread. to get result back in UI Thread start AsyncTask as :

String str_result= new RunInBackGround().execute().get();

这篇关于等到异步任务完成其工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆