使用开源框架进行多线程下载

使用开源框架,多线程下载,

好处,代码简单易懂

需要导入多线程下载的jia包


// 设置下载进度条
final ProgressDialog pd = new ProgressDialog(
SplashActivity.this);
// 设置下载进度条水平方向显示
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pd.show();
// 点击更新,进入多线程下载
HttpUtils http = new HttpUtils();
// 外部储存卡的状态
String state = Environment.getExternalStorageState();
File sdDir = Environment.getExternalStorageDirectory();
// 下载储存的地址
File file = new File(sdDir, SystemClock.uptimeMillis()+ ".apk");
if (state.equals(Environment.MEDIA_MOUNTED)) {


http.download(downloadpath, file.getAbsolutePath(),new RequestCallBack<File>() {


@Override
public void onSuccess(// 下载成功
ResponseInfo<File> fileinfo) {
Toast.makeText(SplashActivity.this,"下载成功", 0).show();
pd.dismiss();
installNewAPP(fileinfo);
}


/**
* 调用系统的覆盖安装程序

* @param fileinfo
*/
private void installNewAPP(
ResponseInfo<File> fileinfo) {
// 安装应用程序APK文件
// 创建意图的对象
Intent intent = new Intent();
intent.setAction("android.intent.action.VIEW");
intent.addCategory("android.intent.category.DEFAULT");
intent.setDataAndType(Uri.fromFile(fileinfo.result),"application/vnd.android.package-archive");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// 开启Activity
startActivity(intent);
loadHome(); // 不选择安装也可跳转到主界面
}


@Override
public void onLoading(long total,long current,boolean isUploading) {// 下载中设置进度条
// 设置进度条最大值
pd.setMax((int) total);
// 设置进度条当前值
pd.setProgress((int) current);
super.onLoading(total, current,
isUploading);
}


@Override
public void onFailure(// 下载失败
HttpException arg0, String arg1) {
Toast.makeText(SplashActivity.this,"下载失败", 0).show();
pd.dismiss();
}
});
} else {
loadHome();
}
}

猜你喜欢

转载自blog.csdn.net/Panda_Kill/article/details/52905159