使用Asynctask显示进度条

时间:2012-06-09 09:30:01

标签: android android-asynctask

我有一个应用程序,我在其中进行函数调用,根据用户输入需要一些时间来计算。我尝试使用AsyncTask实现进度条显示,但是当我在doinbackGround中声明函数调用时,由于它与UI线程交互,因此出错。我为函数调用创建了一个单独的线程,并在UI线程中创建了一个名为AsyncTask的类并同步了它们的时序,即将Asynctask的doinbackground中的休眠时间设置为一个较大的值,以便在此时间内完成函数调用

但这不是一个好的解决方案,因为方法调用完成所花费的时间取决于用户输入,我之前不知道。我还希望我的进度条连续显示而不是离散。我正在为Asynctask调用类和函数调用提供代码。

Asynctask调用Class

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来更新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

请建议一种方法如何实现这一目标

2 个答案:

答案 0 :(得分:3)

使用AsyncTask。它可以与onProgressUpdate和onPostExecute中的UI线程进行交互。 您必须在doInBackground()中继续计算,而在onProgressUpdate()中更新UI。 将Logic类分解为更多方法并按照这样做。

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







}

答案 1 :(得分: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();
    }
}