2013-03-18 37 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