private ProgressDialog progressDialog;
//新建一个File,传入文件夹目录
String SDPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/by/";
File file1 = new File(SDPath);
//判断文件夹是否存在,如果不存在就创建,否则不创建
if (!file1.exists()) {
//通过file的mkdirs()方法创建目录中包含却不存在的文件夹
file1.mkdirs();
}
private void downFile(String url, String destFileDir, String destFileName, int type) {
OkGo.get(url)
.tag(this)
//.headers("header1", "headerValue1")//
//.params("param1", "paramValue1")//
.execute(new FileCallback(destFileDir, destFileName) {
@Override
public void onStart(Request request) {
super.onStart(request);
LogUtils.e("正在下载中。。。。。");
if(type == 1){
progressDialog = new ProgressDialog(mContext);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
// progressDialog.setMax(100);
// progressDialog.setIndeterminate(false);
progressDialog.setCancelable(false);
progressDialog.show();
}
}
@Override
public void onSuccess(Response response) {
LogUtils.e("下载完成");
LogUtils.e("response.body()==" + response.body());
if(type == 0){
//zLoadingDialog.dismiss();
//openPDF(response.body());//打开PDF文件
//Bundle bundle = new Bundle();
//bundle.putString("pdfUrl", response.body().toString());
//startActivity(PDFViewActivity.class, bundle);
}else {
ToastUtils.showLong(destFileName + "下载完成");
// progressDialog.dismiss();
DownTimer();
}
}
@Override
public void onError(Response response) {
super.onError(response);
//zLoadingDialog.dismiss();
ToastUtils.showLong("下载出错");
progressDialog.dismiss();
}
@Override
public void downloadProgress(Progress progress) {
super.downloadProgress(progress);
Log.e("=========:", String.valueOf(progress.fraction));
Message msg = handler.obtainMessage();
msg.what = 1;
msg.arg1 = (int) progress.fraction * 100;
handler.sendMessage(msg);
}
});
}
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case 1:
//进度条的值
int i = msg.arg1;
progressDialog.setProgress(i);
}
// if (msg.arg1 == 100) {
// //ToastUtils.showLong("文件下载成功");
// progressDialog.dismiss();
// }
}
};
private void DownTimer(){
CountDownTimer timer = new CountDownTimer(1000, 1000) {
public void onTick(long millisUntilFinished) {
// txt.setText("倒计时" + millisUntilFinished / 1000 + "秒");
}
public void onFinish() {
progressDialog.dismiss();
}
};
//调用 CountDownTimer 对象的 start() 方法开始倒计时,也不涉及到线程处理
timer.start();
}
要引用的控件
//网络加载框架 implementation 'com.lzy.net:okgo:3.0.4'
//工具类 implementation 'com.blankj:utilcodex:1.31.0'



