简体   繁体   English

Android使用AsyncTask更新UI

[英]Android using AsyncTask to update the UI

I want to know when the asynctask is finished, such that I could update the UI. 我想知道asynctask何时完成,以便可以更新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. 想知道asynctask何时完成,以便我可以更新UI。

Yes, you can use onPostExecute method which call on UI Thread after doInBackground execution is completed. 是的,您可以使用onPostExecute方法,该方法在doInBackground执行完成后调用UI线程。

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 . 如果要在主UI线程上取回数据,请使用AsyncTask.get()方法执行AsyncTask,因为此方法在UI线程上等待,直到doInBackground执行完成。

    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 注意:-您将需要在线程而不是Main UI线程中调用AsyncTask.get() ,否则调用get()方法将冻结Main Thread,直到doInBackground执行完成

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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