0

I don't know the very details of async task,
I recently used async task in my project to get some data from server side and update my UI according to the result,I implimented a progress dialog in the usual way and dismissid it in my onPostExicute method like this

@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) {
    }
     }

But I found some codes done the same thing but perfoming the action on a ui thread like this

@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) {
        }

                }
            });
}

I like to know which one is the best method and what are the differences ?

4

1 回答 1

4

wrong implementation approach of AsyncTask .

protected void onPostExecute(ArrayList<StationSlots> result)

is Already running on MainUiThread so you don't have to write runOnUiThread(new Runnable() in OnPostExecute().

Also publishProgress(); is used to update UI from doInBackground() of AsyncTask as its running on a worker thread. And when you call publishProgress(); form doInBackground() then execution comes on onProgressUpdate(Integer... progress) which will also run on MainUIThread so you don't need any where runOnUiThread(new Runnable() in AsyncTask. (except if you want to update UI from doInBackground() without calling publishProgress();)

于 2013-01-10T06:27:42.450 回答