在android中,从aysnc任务更新UI的最佳方式是什么?

在android中,从aysnc任务更新UI的最佳方式是什么?,android,android-asynctask,Android,Android Asynctask,我不知道异步任务的细节,我最近在我的项目中使用异步任务从服务器端获取一些数据,并根据结果更新我的UI,我以通常的方式实现了一个进度对话框,并在我的OnPostExicite方法中取消了它,如下所示 @Override protected void onPostExecute(ArrayList<StationSlots> result) { try { publishProgress(100); WizardStep4.retStationSlots

我不知道异步任务的细节,
我最近在我的项目中使用异步任务从服务器端获取一些数据,并根据结果更新我的UI,我以通常的方式实现了一个进度对话框,并在我的OnPostExicite方法中取消了它,如下所示

@Override
    protected void onPostExecute(ArrayList<StationSlots> result) {
    try {

    publishProgress(100);
    WizardStep4.retStationSlots =  result;      

        if (WizardStep4.this.dialog != null) {
                WizardStep4.this.dialog.dismiss();
          }
    } catch (Exception e) {
    }
     }
@Override
        protected void onPostExecute(ArrayList<StationSlots> result) {
           runOnUiThread(new Runnable() {

       @Override
       public void run() {
        try {
        publishProgress(100);
        WizardStep4.retStationSlots =  result;              
                if (WizardStep4.this.dialog != null) {
                    WizardStep4.this.dialog.dismiss();
                 }
        } catch (Exception e) {
        }

                }
            });
}
@覆盖
受保护的void onPostExecute(ArrayList结果){
试一试{
出版进度(100);
WizardStep4.RetStationSlot=结果;
if(向导step4.this.dialog!=null){
向导步骤4.this.dialog.discouse();
}
}捕获(例外e){
}
}
但是我发现一些代码做了同样的事情,但是在这样的ui线程上执行操作

@Override
    protected void onPostExecute(ArrayList<StationSlots> result) {
    try {

    publishProgress(100);
    WizardStep4.retStationSlots =  result;      

        if (WizardStep4.this.dialog != null) {
                WizardStep4.this.dialog.dismiss();
          }
    } catch (Exception e) {
    }
     }
@Override
        protected void onPostExecute(ArrayList<StationSlots> result) {
           runOnUiThread(new Runnable() {

       @Override
       public void run() {
        try {
        publishProgress(100);
        WizardStep4.retStationSlots =  result;              
                if (WizardStep4.this.dialog != null) {
                    WizardStep4.this.dialog.dismiss();
                 }
        } catch (Exception e) {
        }

                }
            });
}
@覆盖
受保护的void onPostExecute(ArrayList结果){
runOnUiThread(新的Runnable(){
@凌驾
公开募捐{
试一试{
出版进度(100);
WizardStep4.RetStationSlot=结果;
if(向导step4.this.dialog!=null){
向导步骤4.this.dialog.discouse();
}
}捕获(例外e){
}
}
});
}

我想知道哪种方法是最好的,有什么区别?

异步任务的实现方法错误

受保护的void onPostExecute(ArrayList结果)

已在mainuthread上运行,因此您不必在
OnPostExecute()
中写入
runOnUiThread(new Runnable()


另外,
publishProgress();
用于从AsyncTask的
doInBackground()
更新UI,使其在工作线程上运行。当调用
publishProgress();
form
doInBackground()
时,执行开始于
onProgressUpdate(Integer…progress)
也将在mainuthread上运行,因此在AsyncTask中不需要任何where
rununuithread(new Runnable()
。(除非您希望在不调用
publishProgress();
的情况下从
doInBackground()
更新UI)

异步任务的错误实现方法

受保护的void onPostExecute(ArrayList结果)

已在mainuthread上运行,因此您不必在
OnPostExecute()
中写入
runOnUiThread(new Runnable()

另外,
publishProgress();
用于从AsyncTask的
doInBackground()
更新UI,使其在工作线程上运行。当调用
publishProgress();
form
doInBackground()
时,执行开始于
onProgressUpdate(Integer…progress)
它也将运行在mainuthread上,因此在AsyncTask中不需要任何where
rununuithread(new Runnable()
。(除非您想从
doInBackground()
更新UI,而不调用
publishProgress();

@user370305--非常棒的答案。@user370305--非常棒的答案。