2012-06-09 82 views
1

我有一個應用程序在其中進行函數調用,根據用戶輸入需要一些時間來計算。我嘗試使用AsyncTask來實現進度條顯示,但是當我在doinbackGround中聲明函數調用時,由於它與UI線程交互,所以發生錯誤。我爲函數調用創建了一個單獨的線程,並在稱爲AsyncTask類的UI線程中創建了一個單獨的線程,並同步它們的時間,即將Asynctask的doinbackground中的休眠時間設置爲一個較大的值,以便此時函數調用完成。使用Asynctask顯示進度條

但是這不是一個好的解決方案,因爲方法調用完成所花費的時間取決於用戶輸入,我之前不知道。我也希望我的進度條連續顯示而不是離散的。我提供了Asynctask調用類和函數調用的代碼。

的AsyncTask主叫

package com.integrated.mpr; 

public class Progess extends Activity { 

    static String[] display = new String[Model.n]; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.progress); 
     Thread threada=new Thread(){ 
      public void run() {      
       display = new Logic().finaldata(); 
       // this is the function call  
       } 
      }; 
      threada.start(); 
     new loadSomeStuff().execute(" "); 
    } 

    public class loadSomeStuff extends AsyncTask<String,Integer,String>{ 
     ProgressDialog dialog; 
     protected void onPreExecute(){ 
      dialog = new ProgressDialog(Progess.this);    
          dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 
      dialog.setMax(100); 
      dialog.setTitle("Generating the most sensitive positions"); 
      dialog.show();    
     } 
     @Override 
     protected String doInBackground(String... arg0) { 
       for(int i=0;i<20;i++){ 
       publishProgress(5); 
       try { 
        Thread.sleep(1200);// the timing set to a large value 
       } catch (InterruptedException e) { 
        e.printStackTrace(); 
       } 
      } 
      dialog.dismiss(); 
      return null; 
     } 
    protected void onProgressUpdate(Integer...progress){ 
     dialog.incrementProgressBy(progress[0]); 
    } 
     protected void onPostExecute(String result){ 
      Intent openList = new Intent("com.integrated.mpr.SENSITIVELIST"); 
      startActivity(openList); 
     } 
    } 
} 

功能服用較長時間

package com.integrated.mpr; 
public class Logic { 
    int n = Model.n; 
    int ns = Model.ns; 
    double final_matrix[][] = new double[n][5]; 
    double swap =0; 

    double weightage_matrix[] = new double[n]; 
    double sorted_weightage[] = new double[n]; 
    String display[] = new String[n]; 

    double[] peak_matrix = new double[n]; 
    double[] sd_matrix = new double[n]; 
    double[] rms_matrix = new double[n]; 
    double[] cf_matrix = new double[n]; 
    double[] mean_matrix = new double[n]; 
    int[] sensitive_positions = new int[n]; 
    double[] new_sensitive = new double[n]; 
    int[] sortsensi = new int[n]; 
    public String[] finaldata(){ 

    for(int i=0;i<n;i++){ 
      peak_matrix[i] = Model.timedata[i*5+0]; 
      sd_matrix[i] = Model.timedata[i*5+1]; 
      rms_matrix[i] = Model.timedata[i*5+2]; 
      cf_matrix[i] = Model.timedata[i*5+3]; 
      mean_matrix[i] = Model.timedata[i*5+4]; 
     } 

     // Arrays sorted in asecnding order 
     java.util.Arrays.sort(peak_matrix); 
     java.util.Arrays.sort(sd_matrix); 
     java.util.Arrays.sort(rms_matrix); 
     java.util.Arrays.sort(mean_matrix); 
     java.util.Arrays.sort(cf_matrix); 


     Log.d("matrices", "sorted"); 
    for(int i = 0;i<n;i++){ 
     final_matrix[i][0]= peak_matrix[i]; 
     final_matrix[i][1]= sd_matrix[i]; 
     final_matrix[i][2]= rms_matrix[i]; 
     final_matrix[i][3]= cf_matrix[i]; 
     final_matrix[i][4]= mean_matrix[i]; 
    } 

    Log.d("final ", "matrix"); 
    double temp =0; 

    for(int i=0;i<n;i++){ 
     for(int j=0;j<5;j++){ 
      temp = final_matrix[i][j]; 
      for(int k =0;k<n;k++){ 
       if(temp==Model.timedata[k*5+j]){ 
        weightage_matrix[k] = weightage_matrix[k]+(i+1)*n; 
       } 
      } 
     } 
    } 

    //copying the values into sorted matrix; 

    for(int i=0;i<n;i++){ 
     sorted_weightage[i] = weightage_matrix[i]; 
    } 

    //sorting weighatge matrix in descending order 

     for (int i = 0;i<n; i++) 
      { 
       for (int j = 0 ; j < n-i-1 ; j++) 
       { 
        if (sorted_weightage[j] <sorted_weightage[j+1]) { 
         swap = sorted_weightage[j]; 
         sorted_weightage[j] = sorted_weightage[j+1]; 
         sorted_weightage[j+1] = swap; 
        } 
       } 

      }   
     Log.d("sorted weightage", "matrix"); 
     for(int i =0;i<n;i++){ 
     temp = sorted_weightage[i]; 
     for(int j =0;j<n;j++){ 
      if(temp==weightage_matrix[j]){ 
       sensitive_positions[i]=j+1; 
       } 
      } 
     } 
    RealMatrix pcorrdata = new PearsonsCorrelation().computeCorrelationMatrix(Model.input_matrix); 
    // the above statement takes time depending on the user input 



    for(int i =0;i<n;i++){ 
     for(int j =0;j<n;j++){ 
      if(pcorrdata.getEntry(i, j)<0){ 
       pcorrdata.setEntry(i, j, pcorrdata.getEntry(i, j)*-1); 
      } 

     } 
    } 

    for(int i =0;i<n;i++){ 
     for(int j =0;j<n;j++){ 
      Log.d(" "+i+" "+j, ""+pcorrdata.getEntry(i, j)); 
     } 
    } 

    for(int i =0;i<n;i++){ 
     Log.d("sensitive osition before correlation", ""+sensitive_positions[i]); 
    } 

    int[] perm_sensitive = sensitive_positions; 
    for(int i =0;i<ns;i++){ 
     int temp1 = perm_sensitive[i]-1; 
     if(i+1<n){ 
     for(int j=i+1;j<ns;j++){ 
      int temp2 = perm_sensitive[j]-1; 
        if(pcorrdata.getEntry(temp1, temp2)>0.5){ 
         sensitive_positions =append((temp2)+1,sensitive_positions); 

        } 
       } 
     perm_sensitive = sensitive_positions; 
     Log.d("perm", ""+perm_sensitive[0]); 
     Log.d("perm", ""+perm_sensitive[1]); 
      } 
    } 

    for(int i =0;i<n;i++){ 
     Log.d("values",""+perm_sensitive[i]); 
    } 


    for(int i =0;i<n;i++){ 
     display[i] = Model.posnames[perm_sensitive[i]-1]; 
    } 


    return display; 

    } 
    private int[] append(int j, int[] sensitive_positions) { 
     int[] sort_sensitive = new int[n]; 
     int z = 0; 
     for(int i =0;i<n;i++){ 
      if(sensitive_positions[i]!=j){ 
       sort_sensitive[z]=sensitive_positions[i]; 
       z = z+1; 
      } 
     } 
     sort_sensitive[n-1] = j; 
     return sort_sensitive; 
    } 
} 

使用的AsyncTask的progressupdate到updat UI

package com.integrated.mpr; 
public class Logic extends Activity{ 
    int n = Choose.n; 
    double final_matrix[][] = new double[n][5]; 
    double swap =0; 

    double weightage_matrix[] = {0,0,0,0,0,0,0,0,0,0,0}; 
    double sorted_weightage[] = {0,0,0,0,0,0,0,0,0,0}; 
    static String display[] = new String[Choose.n]; 

    static double[][] input_matrix; 
    double[] peak_matrix; 
    double[] sd_matrix; 
    double[] rms_matrix ; 
    double[] cf_matrix ; 
    double[] mean_matrix ; 
    int[] sensitive_positions ; 
    double[] new_sensitive ; 
    int[] sortsensi ; 
    RealMatrix pcorrdata ; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.progress); 
     String x = "abc"; 
     new loadSomeStuff().execute(x); 
    } 
    public class loadSomeStuff extends AsyncTask<String,Integer,String>{ 
     ProgressDialog dialog; 
     protected void onPreExecute(){ 
      dialog = new ProgressDialog(Logic.this); 
      dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 
      dialog.setMax(100); 
      dialog.setMessage("Computing Most Sensitive Positions"); 
      dialog.show(); 
     } 
     @Override 
     protected String doInBackground(String... params) { 
       publishProgress(25); 
       publishProgress(50); 
       publishProgress(75); 
       publishProgress(100); 
     } 

     private int[] append(int j, int[] sensitive_positions) { 
      int[] sort_sensitive = new int[n]; 
      int z = 0; 
      for(int i =0;i<n;i++){ 
       if(sensitive_positions[i]!=j){ 
        sort_sensitive[z]=sensitive_positions[i]; 
        z = z+1; 
       } 
      } 
      sort_sensitive[n-1] = j; 
      return sort_sensitive; 
     } 

     protected void onProgressUpdate(Integer...progress){ 
      if(progress[0]==25){ 
       Log.d("loop 1 ", "start"); 
       Log.d("now in ", "35 loop"); 
       input_matrix = new double[22050][n]; 
       peak_matrix = new double[n]; 
       sd_matrix = new double[n]; 
       rms_matrix = new double[n]; 
       cf_matrix = new double[n]; 
       mean_matrix = new double[n]; 
       sensitive_positions = new int[n]; 
       new_sensitive = new double[n]; 
       sortsensi = new int[n]; 
       for(int i=0;i<n;i++){ 
        peak_matrix[i] = Choose.timedata[i*5+0]; 
        sd_matrix[i] = Choose.timedata[i*5+1]; 
        rms_matrix[i] = Choose.timedata[i*5+2]; 
        cf_matrix[i] = Choose.timedata[i*5+3]; 
        mean_matrix[i] = Choose.timedata[i*5+4]; 
       } 
       for(int i = 0;i<n;i++){ 
        final_matrix[i][0]= peak_matrix[i]; 
        final_matrix[i][1]= sd_matrix[i]; 
        final_matrix[i][2]= rms_matrix[i]; 
        final_matrix[i][3]= cf_matrix[i]; 
        final_matrix[i][4]= mean_matrix[i]; 
       } 
       //final sorted matrix obtained 
       for(int i =0;i<n;i++){ 
        for(int j=0;j<5;j++){ 
         if(final_matrix[i][j]== new Page1().timedata1[j]){ 
          weightage_matrix[0] = weightage_matrix[0]+(i+1)*24; 
         } 
         else if (final_matrix[i][j]== new Page2().timedata2[j]){ 
          weightage_matrix[1] = weightage_matrix[1]+(i+1)*24; 
         } 
         else if (final_matrix[i][j]== new Page3().timedata3[j]){ 
          weightage_matrix[2] = weightage_matrix[2]+(i+1)*24; 
         } 
         else if (final_matrix[i][j]== new Page4().timedata4[j]){ 
          weightage_matrix[3] = weightage_matrix[3]+(i+1)*24; 
         } 

         else{ 
          weightage_matrix[4] = weightage_matrix[4]+(i+1)*24; 
         } 
       } 
      } 

       Log.d("loop 1 ", "stop"); 
       Log.d("now ", "incrementing"); 
       dialog.incrementProgressBy(15); 
      } 
      else if (progress[0]==50){ 
       Log.d("loop 2 ", "start"); 
      //copying the values into sorted matrix; 
      for(int i=0;i<n;i++){ 
       sorted_weightage[i] = weightage_matrix[i]; 
      } 
      //sorting weighatge matrix in descending order 
      for (int i = 0;i<n; i++){ 
         for (int j = 0 ; j < n-i-1 ; j++){ 
          if (sorted_weightage[j] <sorted_weightage[j+1]) { 
           swap = sorted_weightage[j]; 
           sorted_weightage[j] = sorted_weightage[j+1]; 
           sorted_weightage[j+1] = swap; 
          } 
         }    
      } 
       for(int i =0;i<n;i++){ 
        double temp = sorted_weightage[i]; 
         for(int j =0;j<n;j++){ 
          if(temp==weightage_matrix[j]){ 
          sensitive_positions[i]=j+1; 
          } 
         } 
       } 

       Log.d("loop 2 ", "stop"); 
       dialog.incrementProgressBy(20); 
       //now for correaltion 
      } 

      else if (progress[0] == 75){ 
       // genearting the input matrix for correaltion 

       Log.d("loop 3 ", "start"); 
       for(int i=0;i<n;i++){ 
        for(int j=0;j<22050;j++){ 
         input_matrix[j][i] = new Choose().rawdata[i*22050+j]; 
        } 
       } 

       // now generating correlation matrix of N x n by using pearson correaltion 
       pcorrdata = new PearsonsCorrelation().computeCorrelationMatrix(input_matrix); 
       dialog.incrementProgressBy(35); 
      } 
      else{     
       Log.d("checkng correlation mtrix", "yup");     
       for(int i =0;i<n;i++){ 
        for(int j =0;j<n;j++){       
         if(pcorrdata.getEntry(i, j)<0){ 
          pcorrdata.setEntry(i, j, pcorrdata.getEntry(i, j)*-1); 
         }      
        } 
       }    
       Log.d("now in", "75 l00p"); 
       for(int i =0;i<n;i++){ 
        Log.d("sensitive osition before correlation", ""+sensitive_positions[i]); 
       }     
       Log.d("loop 3 ", "stop"); 
       Log.d("loop 4 ", "start");     
       int[] perm_sensitive = sensitive_positions;     
       if((pcorrdata.getEntry(perm_sensitive[0]-1, perm_sensitive[1]-1))>0.5){ 
        sensitive_positions = append(perm_sensitive[1],sensitive_positions); 
       }     
       perm_sensitive = sensitive_positions;     
       if((pcorrdata.getEntry(perm_sensitive[2]-1, perm_sensitive[3]-1))>0.5){ 
        sensitive_positions = append(perm_sensitive[3],sensitive_positions); 
       }     
       for(int i =0;i<n;i++){ 
        Log.d("values",""+perm_sensitive[i]); 
       } 
       for(int i =0;i<n;i++){ 
        display[i] = new Choose().posnames[perm_sensitive[i]-1]; 
       } 
       Log.d("loop 4 ", "stop"); 
       dialog.incrementProgressBy(20); 
       Log.d("now in ","100 loop"); 
       Intent openList = new Intent("com.integrated.mpr.SENSITIVELIST"); 
       startActivity(openList);     
      } 
     }   
     protected void onPostExecute(String result){ 
      dialog.dismiss(); 
      Intent openList = new Intent("com.integrated.mpr.SENSITIVELIST"); 
      startActivity(openList);     
     } 
    } 
} 

logcat的錯誤

06-09 16:32:05.670: E/AndroidRuntime(8009): FATAL EXCEPTION: AsyncTask #4 
06-09 16:32:05.670: E/AndroidRuntime(8009): java.lang.RuntimeException: An error occured while executing doInBackground() 
06-09 16:32:05.670: E/AndroidRuntime(8009):  at android.os.AsyncTask$3.done(AsyncTask.java:200) 
06-09 16:32:05.670: E/AndroidRuntime(8009):  at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273) 
06-09 16:32:05.670: E/AndroidRuntime(8009):  at java.util.concurrent.FutureTask.setException(FutureTask.java:124) 
06-09 16:32:05.670: E/AndroidRuntime(8009):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307) 
06-09 16:32:05.670: E/AndroidRuntime(8009):  at java.util.concurrent.FutureTask.run(FutureTask.java:137) 
06-09 16:32:05.670: E/AndroidRuntime(8009):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068) 
06-09 16:32:05.670: E/AndroidRuntime(8009):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561) 
06-09 16:32:05.670: E/AndroidRuntime(8009):  at java.lang.Thread.run(Thread.java:1096) 
06-09 16:32:05.670: E/AndroidRuntime(8009): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() 
06-09 16:32:05.670: E/AndroidRuntime(8009):  at android.os.Handler.<init>(Handler.java:121) 
06-09 16:32:05.670: E/AndroidRuntime(8009):  at android.app.Activity.<init>(Activity.java:679) 
06-09 16:32:05.670: E/AndroidRuntime(8009):  at com.integrated.mpr.Logic.<init>(Logic.java:13) 
06-09 16:32:05.670: E/AndroidRuntime(8009):  at com.integrated.mpr.Progess$loadSomeStuff.doInBackground(Progess.java:53) 
06-09 16:32:05.670: E/AndroidRuntime(8009):  at com.integrated.mpr.Progess$loadSomeStuff.doInBackground(Progess.java:1) 
06-09 16:32:05.670: E/AndroidRuntime(8009):  at android.os.AsyncTask$2.call(AsyncTask.java:185) 
06-09 16:32:05.670: E/AndroidRuntime(8009):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305) 
06-09 16:32:05.670: E/AndroidRuntime(8009):  ... 4 more 

請提出一個方法,我該如何做到這一點?

回答

3

使用AsyncTask。它可以在onProgressUpdate和onPostExecute中與UI線程交互。 你必須保持你的計算在doInBackground()和只有更新onProgressUpdate()中的用戶界面。 將你的邏輯類分解成更多的方法,並像這樣做。

public class LogicAsync extends AsyncTask<Void, Integer, Void> { 

    @Override 
    protected Void doInBackground(Void... p) { 
     Logic logic = new Logic(); 
     logic.loadArrays(); 
     publishProgress(10); //10% done 

     try { 
      Thread.sleep(1000); 
     } catch { 
     } 

     logic.sortArraysAndMatrix(); 
     publishProgress(20); //20% done 
     logic.copyAndSortWeightages(); 
     publishProgress(30); 
     logic.finalData(); 
     publishProgress(100); 
    } 

    @Override 
    protected void onProgressUpdate(Integer... progress) { 
     updateUIWithPercent(progress[0]); 
    } 
} 

這是一個修改後的邏輯類,其中有一些分解方法。

package com.integrated.mpr; 

import org.apache.commons.math.linear.RealMatrix; 
import org.apache.commons.math.stat.correlation.Covariance; 
import org.apache.commons.math.stat.correlation.PearsonsCorrelation; 
import org.apache.commons.math.util.FastMath; 

import android.util.Log; 

public class Logic { 

    int n = Model.n; 
    int ns = Model.ns; 
    double final_matrix[][] = new double[n][5]; 
    double swap =0; 

    double weightage_matrix[] = new double[n]; 
    double sorted_weightage[] = new double[n]; 
    String display[] = new String[n]; 

    double[] peak_matrix = new double[n]; 
    double[] sd_matrix = new double[n]; 
    double[] rms_matrix = new double[n]; 
    double[] cf_matrix = new double[n]; 
    double[] mean_matrix = new double[n]; 
    int[] sensitive_positions = new int[n]; 
    double[] new_sensitive = new double[n]; 
    int[] sortsensi = new int[n]; 

    public void loadArrays() { 
     for(int i=0;i<n;i++){ 
      peak_matrix[i] = Model.timedata[i*5+0]; 
      sd_matrix[i] = Model.timedata[i*5+1]; 
      rms_matrix[i] = Model.timedata[i*5+2]; 
      cf_matrix[i] = Model.timedata[i*5+3]; 
      mean_matrix[i] = Model.timedata[i*5+4]; 
     } 
    } 

    public void sortArraysAndMatrix() { 
     // Arrays sorted in asecnding order 
     java.util.Arrays.sort(peak_matrix); 
     java.util.Arrays.sort(sd_matrix); 
     java.util.Arrays.sort(rms_matrix); 
     java.util.Arrays.sort(mean_matrix); 
     java.util.Arrays.sort(cf_matrix); 

     Log.d("matrices", "sorted"); 
     for(int i = 0;i<n;i++){ 
      final_matrix[i][0]= peak_matrix[i]; 
      final_matrix[i][1]= sd_matrix[i]; 
      final_matrix[i][2]= rms_matrix[i]; 
      final_matrix[i][3]= cf_matrix[i]; 
      final_matrix[i][4]= mean_matrix[i]; 
     } 
    } 

    public void copyAndSortWeightages() { 
     Log.d("final ", "matrix"); 
     double temp =0; 

     for(int i=0;i<n;i++){ 
      for(int j=0;j<5;j++){ 
       temp = final_matrix[i][j]; 
       for(int k =0;k<n;k++){ 
        if(temp==Model.timedata[k*5+j]){ 
         weightage_matrix[k] = weightage_matrix[k]+(i+1)*n; 
        } 
       } 
      } 
     } 

     //copying the values into sorted matrix; 

     for(int i=0;i<n;i++){ 
      sorted_weightage[i] = weightage_matrix[i]; 
     } 

     //sorting weighatge matrix in descending order 

     for (int i = 0;i<n; i++) 
      { 
       for (int j = 0 ; j < n-i-1 ; j++) 
       { 
        if (sorted_weightage[j] <sorted_weightage[j+1]) { 
         swap = sorted_weightage[j]; 
         sorted_weightage[j] = sorted_weightage[j+1]; 
         sorted_weightage[j+1] = swap; 
        } 
       } 

      } 


     Log.d("sorted weightage", "matrix"); 
    } 


    public String[] finaldata(){ 

     for(int i =0;i<n;i++){ 
     temp = sorted_weightage[i]; 
     for(int j =0;j<n;j++){ 
      if(temp==weightage_matrix[j]){ 
       sensitive_positions[i]=j+1; 
       } 
      } 
     } 

     RealMatrix pcorrdata = new PearsonsCorrelation().computeCorrelationMatrix(Model.input_matrix); 
     // the above statement takes time depending on the user input 



    for(int i =0;i<n;i++){ 
     for(int j =0;j<n;j++){ 

      if(pcorrdata.getEntry(i, j)<0){ 
       pcorrdata.setEntry(i, j, pcorrdata.getEntry(i, j)*-1); 
      } 

     } 
    } 

    for(int i =0;i<n;i++){ 
     for(int j =0;j<n;j++){ 
      Log.d(" "+i+" "+j, ""+pcorrdata.getEntry(i, j)); 
     } 
    } 

    for(int i =0;i<n;i++){ 
     Log.d("sensitive osition before correlation", ""+sensitive_positions[i]); 
    } 

    int[] perm_sensitive = sensitive_positions; 
    for(int i =0;i<ns;i++){ 
     int temp1 = perm_sensitive[i]-1; 
     if(i+1<n){ 
     for(int j=i+1;j<ns;j++){ 
      int temp2 = perm_sensitive[j]-1; 
        if(pcorrdata.getEntry(temp1, temp2)>0.5){ 
         sensitive_positions =append((temp2)+1,sensitive_positions); 

        } 
       } 
     perm_sensitive = sensitive_positions; 
     Log.d("perm", ""+perm_sensitive[0]); 
     Log.d("perm", ""+perm_sensitive[1]); 
      } 
    } 

    for(int i =0;i<n;i++){ 
     Log.d("values",""+perm_sensitive[i]); 
    } 


    for(int i =0;i<n;i++){ 
     display[i] = Model.posnames[perm_sensitive[i]-1]; 
    } 


    return display; 

    } 


    private int[] append(int j, int[] sensitive_positions) { 
     // TODO Auto-generated method stub 

     int[] sort_sensitive = new int[n]; 
     int z = 0; 
     for(int i =0;i<n;i++){ 
      if(sensitive_positions[i]!=j){ 
       sort_sensitive[z]=sensitive_positions[i]; 
       z = z+1; 
      } 
     } 
     sort_sensitive[n-1] = j; 
     return sort_sensitive; 
    } 







} 
+0

並把代碼logic.mehtod1,邏輯。方法2中的onProgress更新對嗎? –

+0

看到我更新了我的帖子,問題是運行時出現空白屏幕並且UI掛起,即我們只在uI中執行長操作。 Plz幫助 –

+1

我編輯它。你必須在doInBackground中保留所有的邏輯計算。您*只*更新onProgressUpdate中的UI。在onProgressUpdate中完全沒有邏輯。 –

1

您可以顯示在onPreExecute()方法的進度對話框是這樣的:

private class SaveProfileData extends AsyncTask<Void, Void, Void> { 

    public SaveProfileData(){ 
     super(); 
    } 

    @Override 
    protected void onPreExecute(){ 
     pd = ProgressDialog.show(YourActivity.this, null, YourActivity.this.getString(R.string.lodingMessage), true); 
    } 

    @Override 
    protected Void doInBackground(Void... params) { 
     runOnUiThread(new Runnable() { 
      public void run() { 
       yourMethod(); 
      }}); 
     return null; 
    } 

    @Override 
    protected void onPostExecute(Void result){ 
     pd.dismiss(); 
    } 
}