有一个非常好的库可以处理所有这些,并且在网络由于某种未知原因而脱机的情况下具有恢复功能。android 库称为 PRDownloader,可在 Github 上获取:
https ://github.com/MindorksOpenSource/PRDownloader
首先你需要把它添加到你的 gradle 文件中,像这样(当前版本 os 0.6.0)
implementation 'com.mindorks.android:prdownloader:0.6.0'
之后,可以在我开发的应用程序中实现的类中使用它,如下所示。它使用进度对话框并显示下载的百分比,并且如上所述,当网络脱机或用户退出应用程序时,它能够在以后获取下载。可以根据具体需要进行调整,代码如下:
public class DownloadManager {
private MainActivity activity;
private ProgressDialog progressDialog;
private int downloadStatus;
private String filename="";
private static String dirPath;
private String URL = "https://someurl.com/dummyfile.pdf";
public DownloadManager(MainActivity _activity){
this.activity=_activity;
dirPath = Utils.getRootDirPath(activity);
downloadStatus=0;
}
public void setDownloadUrl(String url){
this.URL=url;
}
public String getFileNameDirPath(){
return dirPath+"/"+filename;
};
public void setFilename(String filename){
this.filename=filename;
}
public String getFilename(){
return this.filename;
}
public String getDirectoryPath(){
return this.filename;
}
public void startFileDownload(){
if (Status.RUNNING == PRDownloader.getStatus(downloadStatus)) {
PRDownloader.pause(downloadStatus);
return;
}
this.progressDialog = new ProgressDialog(this.activity);
this.progressDialog.setIndeterminate(true);
this.progressDialog.setTitle("Title");
this.progressDialog.setMessage("Downloading file...");
this.progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
this.progressDialog.setCancelable(false);
this.progressDialog.setMax(100);
this.progressDialog.show();
if (Status.PAUSED == PRDownloader.getStatus(downloadStatus)) {
PRDownloader.resume(downloadStatus);
return;
}
downloadStatus = PRDownloader.download(URL, dirPath, this.filename)
.build()
.setOnStartOrResumeListener(new OnStartOrResumeListener() {
@Override
public void onStartOrResume() {
progressDialog.setIndeterminate(false);
}
})
.setOnPauseListener(new OnPauseListener() {
@Override
public void onPause() {
}
})
.setOnCancelListener(new OnCancelListener() {
@Override
public void onCancel() {
progressDialog.setProgress(0);
downloadStatus = 0;
progressDialog.setIndeterminate(false);
progressDialog.dismiss();
}
})
.setOnProgressListener(new OnProgressListener() {
@Override
public void onProgress(Progress progress) {
long progressPercent = progress.currentBytes * 100 / progress.totalBytes;
progressDialog.setProgress((int) progressPercent);
progressDialog.setIndeterminate(false);
}
})
.start(new OnDownloadListener() {
@Override
public void onDownloadComplete() {
progressDialog.dismiss();
activity.fragmentManagement.setCurrentFragment("MapFragment");
}
@Override
public void onError(Error error) {
Toast.makeText(activity, "Error downloading file", Toast.LENGTH_SHORT).show();
progressDialog.setProgress(0);
downloadStatus = 0;
progressDialog.setIndeterminate(false);
progressDialog.dismiss();
}
});
}
public static final class Utils {
private Utils() {
// no instance
}
public static String getRootDirPath(Context context) {
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
File file = ContextCompat.getExternalFilesDirs(context.getApplicationContext(),
null)[0];
return file.getAbsolutePath();
} else {
return context.getApplicationContext().getFilesDir().getAbsolutePath();
}
}
public static String getProgressDisplayLine(long currentBytes, long totalBytes) {
return getBytesToMBString(currentBytes) + "/" + getBytesToMBString(totalBytes);
}
private static String getBytesToMBString(long bytes){
return String.format(Locale.ENGLISH, "%.2fMb", bytes / (1024.00 * 1024.00));
}
}
}