Android使用AsyncTask更新UI
我想知道异步任务何时完成,以便更新UI 有人能帮忙吗????谢谢Android使用AsyncTask更新UI,android,android-asynctask,android-ui,Android,Android Asynctask,Android Ui,我想知道异步任务何时完成,以便更新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
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何时完成,以便我可以更新
用户界面
是的,您可以使用onPostExecute
方法,该方法在doInBackground
执行完成后调用UI线程
若您想在主UI线程上获取数据,那个么请使用该方法执行AsyncTask,因为该方法使UI线程等待,直到doInBackground
执行完成
String str_result= new DLfromServer().execute("url link").get();
// now use str_result for Updating result
注意:-您需要在线程内而不是在主UI线程内调用
AsyncTask.get()
,否则调用get()方法将冻结主线程,直到doInBackground
执行完成您可以给出如何在线程内调用AsyncTask.get()的示例吗?Asynctask实际上是一个类,我从一个片段调用它。。。这是否已经意味着它不在主线程上?@user1702061:只需创建一个类似java的线程,然后移动String str_result=new DLfromServer().execute(“url链接”).get()代码>线程的内部运行方法。