androidandroid-asynctaskprogress-barpercentageonupdate

Progressbar for two or more operations


I have this code in doInBackground method of an AsyncTask:

    for (int i = 0; i < lenght; i++) {
        // Do something

        count++;
        publishProgress(count * 100 / lenght);
    }

and all works fine. If i add another operation, how to reflect this with the progress bar?

Now i have this code:

    for (int i = 0; i < lenght1; i++) {
        // Do something

        count++;
        publishProgress(count * 100 / lenght1);
    }

    for (int i = 0; i < lenght2; i++) {
        // Do something 

        count++;
        publishProgress(count * 100 / lenght2);
    }

How to make the bar start from 0 when operation 1 starts and finish at 100 when operation 2 ends? I tried to change count*100 to count*50 but it seems not to be the right way...


Solution

  • Find an arithmetic formula involving length1 and length2, to publish the progress accordingly.

    For example, if you want to give same weight to each increase of count and 100% means count == length1+length2, then you can use something like this: publishProgress(count * 100 / (length1+length2));