0

UI を更新できるように、asynctask がいつ終了するかを知りたいです。

誰か助けてくれませんか???? ありがとう!!!

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;
}
4

1 に答える 1

1

UIを更新できるように、asynctaskがいつ終了するか知りたいです。

はい、実行完了onPostExecute後にUI Thread上で呼び出すメソッドを利用できます。doInBackground

メイン UI スレッドでデータを取得する場合は、AsyncTask.get()メソッドを使用して AsyncTask を実行します。このメソッドは、doInBackground実行が完了するまで UI スレッドで待機するためです。

    String str_result= new DLfromServer().execute("url link").get();
    // now use str_result for Updating result

注 :-AsyncTask.get()メイン UI スレッドの代わりにスレッド内で呼び出す必要があります。そうしないと、get() メソッドを呼び出すと、doInBackground実行が完了するまでメイン スレッドがフリーズします。

于 2013-03-18T04:48:37.020 に答える