并行下载并通过 AsyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR) 获取单独的下载进度
Parallel downloading and get individual download progress via AsyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR)
我正在使用
并行下载文件
asyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR)
我想要我添加的每个任务的单独进度。
ArrayList<AsyncTask> mListAsync = new ArrayList<>();
final DownloadTask downloadTask = new DownloadTask(mContext, name);
downloadTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,mVideoUrl.trim());
mListAsync.add(downloadTask );
以上是我用来下载文件的示例代码,我确实维护了一个数组列表来获取队列中添加了多少任务。
有什么方法可以让我在线程池中添加单独的 AsyncTask 进度更新。
假设您的 DownloadTask class 使用 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...)
编辑:
一些带有回调的示例代码:
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);
}
我正在使用
并行下载文件asyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR)
我想要我添加的每个任务的单独进度。
ArrayList<AsyncTask> mListAsync = new ArrayList<>();
final DownloadTask downloadTask = new DownloadTask(mContext, name);
downloadTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,mVideoUrl.trim());
mListAsync.add(downloadTask );
以上是我用来下载文件的示例代码,我确实维护了一个数组列表来获取队列中添加了多少任务。
有什么方法可以让我在线程池中添加单独的 AsyncTask 进度更新。
假设您的 DownloadTask class 使用 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...)
编辑: 一些带有回调的示例代码:
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);
}