来源:网易云课堂Android极客班第八次作业练习
练习内容: 多线程 asyncTask handler
首先获取到目标文件的大小,然后在磁盘上申请一块空间用于保存目标文件,接着把目标文件分割成n份,分别创建线程下载.
//使用目标文件的下载链接作为发起网络请求
mUrl = new URL("http://download.sj.qq.com/upload/connAssitantDownload/upload/MobileAssistant_1.apk");
//注意不是URLConnection, URLConnection是抽象类,HttpURLConnection是它的子类
HttpURLConnection urlConnection = (HttpURLConnection) mUrl.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setConnectTimeout(5000);
//获取目标文件的大小
mContentLength = urlConnection.getContentLength();
mFile = new File(Environment.getExternalStorageDirectory(), getFileName(params[0]));
if (mFile.exists()) {//如果文件已经存在,删除
mFile.delete();
}
RandomAccessFile randomFile = new RandomAccessFile(mFile, "rw");
//设置文件大小
randomFile.setLength(mContentLength);
int blockSize = mContentLength / 3;
for (int i = 0; i < 3; i++) {
int begin = i * blockSize;
int end = (i + 1) * blockSize - 1;
if (i == 2) {
end = mContentLength;
}
//HashMap<String, Integer> map = new HashMap<>();
//map.put("begin", begin);
//map.put("end", end);
//map.put("finished", 0);
//threadList.add(map);
//new Thread
new Thread(new DownloadThread(i, begin, end, mFile, mUrl)).start();
}
在每个线程进行下载的过程中,每次写入文件的时候,记录已经下载了多少内容,重新开始下载时,从上次结束的位置继续下载.
使用HashMap存储该线程下载文件的起始位置,结束位置和已完成大小,并使用一个ArrayList存储各线程数据
private List<HashMap<String, Integer>> threadList = new ArrayList<>();
HashMap<String, Integer> map = new HashMap<>();
map.put("begin", begin);//开始位置
map.put("end", end);//结束位置
map.put("finished", 0);//已完成
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
//指定起止位置
urlConnection.setRequestProperty("Range", "bytes=" + begin + "-" + end);
while ((len = is.read(buf)) != -1 && isDownloading) {
randomFile.write(buf, 0, len);
updateProgress(len);
//更新map中的finished字段的值
map.put("finished", map.get("finished") + len);
}
for (int i = 0; i < threadList.size(); i++) {
HashMap<String, Integer> map = threadList.get(i);
new Thread(new DownloadThread(i, map.get("begin") + map.get("finished"), map.get("end"), mFile, mUrl)).start();
}
自定义handler
public static class DownloadHandler extends android.os.Handler {
public final WeakReference<MainActivity> mActivity;
public DownloadHandler(MainActivity activity) {
mActivity = new WeakReference<MainActivity>(activity);
}
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
MainActivity activity = mActivity.get();
switch (msg.what) {
case 0:
int progress = (int) msg.obj;
activity.getProgressBar().setProgress(progress);
if (progress == 100) {
// Toast.makeText(activity, "下载成功",Toast.LENGTH_SHORT).show();
activity.getDownloadButton().setText("下载成功");
}
}
}
}
更新UI的方法
//使用synchronized
synchronized private void updateProgress(int len) {
total += len;
int temp = total * 100 / mContentLength;
mDownloadHandler.obtainMessage(0, temp).sendToTarget();
}
完整代码github地址:https://github.com/zhangbz/MultithreadingDownloadDemo