问题描述:添加多线程下载任务
解决方案:增加线程池,通过线程池去启动下载任务
1、DownloadService
onCreate()方法中添加线程池
ExecutorService mExecutors = Executors.newCachedThreadPool();
startDownload()方法中启动任务时改成线程池启动线程
private void startDownload(DownloadEntry entry) {
DownloadTask task = new DownloadTask(entry);
mDownloadingTasks.put(entry.id, task);
mExecutors.execute(task);
}
2、DownloadTask
实现Runnable方法
在run方法中调用start()方法
@Override
public void run() {
start();
}
模拟下载过程中,下载的起始位置改成当前长度
for (int i = entry.currentLength; i < entry.totalLength; i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (isPaused || isCancelled) {
entry.status = isPaused ? DownloadEntry.DownloadStatus.pause : DownloadEntry.DownloadStatus.cancel;
DataChanger.getInstance().postStatus(entry);
// TODO if cancelled ,delete related file;
return;
}
i += 1024;
entry.currentLength += 1024;
DataChanger.getInstance().postStatus(entry);
}
3、activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center" >
<Button
android:id="@+id/mDownloadBtn"
android:layout_width="100dp"
android:layout_height="60dp"
android:text="download" />
<Button
android:id="@+id/mDownloadPauseBtn"
android:layout_width="100dp"
android:layout_height="60dp"
android:text="pause" />
<Button
android:id="@+id/mDownloadCancelBtn"
android:layout_width="100dp"
android:layout_height="60dp"
android:text="cancel" />
</LinearLayout>
4、MainActivity
获取按钮
mDowmloadBtn = (Button)findViewById(R.id.mDownloadBtn);
mDownloadPauseBtn = (Button)findViewById(R.id.mDownloadPauseBtn);
mDownloadCancel = (Button)findViewById(R.id.mDownloadCancelBtn);
mDowmloadBtn.setOnClickListener(this);
mDownloadPauseBtn.setOnClickListener(this);
mDownloadCancel.setOnClickListener(this);
实例化DownloadManager
mDownloadManager = DownloadManager.getInstance(this);
在onResume()方法中注册观察者
mDownloadManager.addObserver(watcher);
在onPause()方法中注销观察者
mDownloadManager.removeObserver(watcher);
观察者匿名内部类中notifyUpdate方法
private DataWatcher watcher = new DataWatcher() {
@Override
public void notifyUpdate(DownloadEntry data) {
entry = data;
// 取消操作时,下载实例改成空
if (entry.status == DownloadEntry.DownloadStatus.cancel)
entry = null;
Trace.d(data.toString());
}
};
onClick方法
@Override
public void onClick(View v) {
// 模拟下载Entry
if (entry == null) {
entry = new DownloadEntry();
entry.name = "test.jpg";
entry.url = "http://api.stay4it.com/uploads/test.jpg";
entry.id = "1";
}
switch (v.getId()) {
case R.id.mDownloadBtn: // 开始下载
mDownloadManager.add(entry);
break;
case R.id.mDownloadPauseBtn: // 暂停下载和恢复下载
if (entry.status == DownloadEntry.DownloadStatus.downloading) {
mDownloadManager.pause(entry);
} else if (entry.status == DownloadEntry.DownloadStatus.pause) {
mDownloadManager.resume(entry);
}
break;
case R.id.mDownloadCancelBtn: // 取消下载
mDownloadManager.cancel(entry);
break;
default:
break;
}
}
相关知识来源:
stay老兄的有心课堂-自己动手写多任务下载框架