[英]Android using AsyncTask to update the UI
我想知道asynctask何时完成,以便可以更新UI。
有人可以帮忙吗? 谢谢!!!
public String[] getinfo(int id) {
String[] QueueNo = null;
try {
new DLfromServer().execute("url link"); // this is the asynctask
// want to do stuff here after the asynctask is completed
// actually I want to return a string result after the task is finished....
}
} catch (JSONException e) {
Log.e("GetJSON", "Err:", e);
}
return QueueNo;
}
想知道asynctask何时完成,以便我可以更新UI。
是的,您可以使用onPostExecute
方法,该方法在doInBackground
执行完成后调用UI线程。
如果要在主UI线程上取回数据,请使用AsyncTask.get()方法执行AsyncTask,因为此方法在UI线程上等待,直到doInBackground
执行完成。
String str_result= new DLfromServer().execute("url link").get();
// now use str_result for Updating result
注意:-您将需要在线程而不是Main UI线程中调用AsyncTask.get()
,否则调用get()方法将冻结Main Thread,直到doInBackground
执行完成
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.