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