AsyncTask queue and its entire progress

In your AsyncTask doInBackground() method put your code that downloads all your objects and post updates of the progress bar using a handler. Take a look at this example:



http://developer.android.com/reference/android/widget/ProgressBar.html



The following code goes inside doInBackground():



while (mProgressStatus < 100) {
mProgressStatus = doWork();

// Update the progress bar
mHandler.post(new Runnable() {
public void run() {
mProgress.setProgress(mProgressStatus);
}
});
}

AsyncTask isn't what you should be using for long running downloads. AsyncTasks are really about the UI, and it's important to know that the user navigating away or even rotating their screen will kill the download.



You should use a service to perform the download, so put all of your download code into an IntentService and update some kind of persistant storage to indicate progress (a couple variables in shared preferences would do fine (on to indicate current state, the other to indicate progress)).



You can then use your activity to fire up the service, and then monitor the progress of the downloads via the persistant storage or some callback passed via the intent (persistant storage is probably easiest).

AsyncTask is very much what you're looking for. That's not really true that you can only update for the current Object. What that means is that you can update for the current AsyncTask.



Below is the onProgressUpdate method.



protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
Log.i(logTag, "onProgressUpdate: " + String.valueOf(values[0]));

View view = getView();
if(view != null)
((ProgressBar)view.findViewById(R.id.progressBar)).setProgress(values[0]);
}


You see it takes any number of integers as the arguments. In your publishProgress method, you could call onProgressUpdate(itemProgress, totalProgress) and then update 2 different progress bars accordingly in onProgressUpdate.



You do all the downloading in doInBackground and each time you want to update the progress bar/bars, call publishProgress which passes your progress ints to onProgressUpdate.

AsyncTask is exactly what you are looking for, you can queue depending on the API an amount of tasks, im going to check the Asynctask implementation if there is a getter for the queue size, but if not you can make a list of asyncTask and update when its finished.



If you queue 15 tasks you can set your progress bar to 15 max and on each onPostExecute increment the progressBar :)



I will check the queue later and edit my answer.



Regards.

Of course an AsyncTask can do the job. Just put all your files in a asyncTask which display the progress.



class InitTask extends AsyncTask {
private List mNbFiles;

public InitTask(List filesURL)
{
super();
mFilesURL = filesURL;
}

@Override
protected void onPreExecute() {
//show and init your progress bar here...
super.onPreExecute();
}

@Override
protected Boolean doInBackground(Void... params) {

for(int i = 0 ; i < mNbFiles.size() ; ++i){
//download one file ...
publishProgress(i);
}
}
}


I hope this will help you. Comment if you have any question.


More: