kumar piyush
kumar piyush

Reputation: 173

displaying progressbar using Asynctask

I have an application in which I make a function call that takes some time to compute depending on the user input. I tried to implement a progress bar display using AsyncTask, but when I declared the function call in doinbackGround it gave an error since it interacted with UI thread. I created a separated thread for the function call and in the UI thread called the AsyncTask class and synchronized their timings, i.e. set the sleep time in doinbackground of Asynctask to a large value such that the function call gets completed in that time.

But this not a good solution since the time taken by the method call to complete is dependent on the user input, that I dont know before hand. I also want my progress bar to be displayed continuously and not discrete. I am providing the code for Asynctask calling class and the funcion call.

Asynctask calling 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);
        }
    }
}

The function taking long time

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

using progressupdate of Asynctask to 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 Error

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

Please suggest a method how can I achieve this???

Upvotes: 1

Views: 4348

Answers (2)

Yogesh Somani
Yogesh Somani

Reputation: 2624

You can show the progress dialog in the onPreExecute() method like this:

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

Upvotes: 1

Zaid Daghestani
Zaid Daghestani

Reputation: 8615

Use an AsyncTask. It can interact with UI thread in onProgressUpdate and onPostExecute. You have to keep your calculations in doInBackground() and only update the UI in onProgressUpdate(). Break up your Logic class into more methods and do it like this.

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

Here's a modified logic class with some broken up methods.

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







}

Upvotes: 3

Related Questions