我们在github上搜索xutils即可找到开源项目xutils,下载拷贝到项目中。地址是https://github.com/wyouflf/xUtils.git。
使用开源项目xutils实现多线程断点下载,只需三步:
1. 导入下载好的xUtils.jar包;
2. 实例化一个HttpUtils对象http;
3. 调用http的download方法.
代码如下所示:
HttpUtils http = new HttpUtils();
http.download(path, "/mnt/sdcard/xxx.exe", true, new RequestCallBack<File>() {
@Override
public void onSuccess(ResponseInfo<File> arg0) {
Toast.makeText(MainActivity.this, "下载成功", 0).show();
}
@Override
public void onLoading(long total, long current, boolean isUploading) {
//ProgressBar进度条更新
pb0.setMax((int) total);
pb0.setProgress((int) current);
super.onLoading(total, current, isUploading);
}
@Override
public void onFailure(HttpException arg0, String arg1) {
Toast.makeText(MainActivity.this, "下载失败"+arg1, 0).show();
}
});