Vir Rajpurohit
Vir Rajpurohit

Reputation: 1937

Parallel downloading and get individual download progress via AsyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR)

I am working on parallel downloading of files using

asyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR)

I want the individual progress of the every Task I add.

ArrayList<AsyncTask> mListAsync = new ArrayList<>();
final DownloadTask downloadTask = new DownloadTask(mContext, name);
downloadTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,mVideoUrl.trim());
mListAsync.add(downloadTask );

Above is the sample code I use to download file and I do maintain an arraylist to get how many tasks has been added in que.

Is there any way that I can get the individual AsyncTask progress update added in Thread Pool.

Upvotes: 0

Views: 173

Answers (1)

JacquesBauer
JacquesBauer

Reputation: 266

Assuming your DownloadTask class uses the https://developer.android.com/reference/android/os/AsyncTask.html#publishProgress(Progress...) method, you can get the current progress in a callback from https://developer.android.com/reference/android/os/AsyncTask.html#onProgressUpdate(Progress...)

EDIT: Some sample code with a callback:

public class SampleTask extends AsyncTask<Void, Integer, String> {

    private final int id;
    private final ProgressCallback callback;

    public SampleTask(int uniqueId, ProgressCallback callback){
        this.id = uniqueId;
        this.callback = callback;
    }

    @Override
    protected String doInBackground(Void... voids) {
        // do work and call publish progress in here
        for(int i = 0; i <= 100; i++) {
            try {
                Thread.sleep(500);
            } catch (InterruptedException e){
                e.printStackTrace();
            }
            publishProgress(i);
        }
        return null;
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        //handle progress updates from in here
        callback.onProgress(id, values[0]);
    }
}

public interface ProgressCallback{
    void onProgress(int uniqueId, int progress);
}

Upvotes: 1

Related Questions