简体   繁体   中英

Android using AsyncTask to update the UI

I want to know when the asynctask is finished, such that I could update the UI.

Can anybody help???? thanks!!!

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

want to know when the asynctask is finished, such that I could update the UI.

Yes, you can use onPostExecute method which call on UI Thread after doInBackground execution is completed.

if you want to get data back on Main UI Thread then use AsyncTask.get() method for executing AsyncTask because this method make wait on UI Thread until doInBackground execution is completed .

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

NOTE :- you will need to call AsyncTask.get() inside an Thread instead of Main UI Thread otherwise calling of get() method will freeze Main Thread until doInBackground execution is complete

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM