2013-03-18 171 views
0

我要当的AsyncTask完成知道,这样我可以更新UI。的Android使用的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; 
} 

回答

1

希望在的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 

注: -你将需要调用AsyncTask.get()一个线程,而不是主UI线程否则调用get内()方法将冻结主线程,直到doInBackground执行完成

+0

你能介绍给例子如何在线程中调用AsyncTask.get()? Asynctask实际上是一个类,我从一个片段调用它......这是否意味着它不在主线程上? – 2013-03-18 05:10:36

+0

@ user1702061:只需创建一个线程像Java和移动'字符串str_result =新DLfromServer()执行( “URL链接”)获得();'线程内运行方法。 – 2013-03-18 05:12:41