安卓文件下载库 | PRDownloader
2017-12-05 本文已影响228人
5afd372c86ba
名称 | PRDownloader |
---|---|
语言 | Android |
平台 | GitHub |
作者 | MindorksOpenSource |
链接 | 点此进入 |
今天推荐的是一款具备完善功能的安卓文件下载库——PRDownloader 。
特点
-
支持下载任意文件;
-
支持暂停和恢复下载;
-
支持下载大文件;
-
使用简单;
-
提供多个接口,支持回调开始下载、下载中,下载结束,出现错误各种情况;
-
支持适当的下载请求取消;
-
支持并行下载;
使用方法
- 添加依赖
compile 'com.mindorks.android:prdownloader:0.2.0'
- 添加权限
<uses-permission android:name="android.permission.INTERNET" />
- 在Application中初始化
PRDownloader.initialize(getApplicationContext());
// Enabling database for resume support even after the application is killed:
PRDownloaderConfig config = PRDownloaderConfig.newBuilder()
.setDatabaseEnabled(true)
.build();
PRDownloader.initialize(getApplicationContext(), config);
// Setting timeout globally for the download network requests:
PRDownloaderConfig config = PRDownloaderConfig.newBuilder()
.setReadTimeout(30_000)
.setConnectTimeout(30_000)
.build();
PRDownloader.initialize(getApplicationContext(), config);
- 请求下载文件
int downloadId = PRDownloader.download(url, dirPath, fileName)
.build()
.setOnStartOrResumeListener(new OnStartOrResumeListener() {
@Override
public void onStartOrResume() {
}
})
.setOnPauseListener(new OnPauseListener() {
@Override
public void onPause() {
}
})
.setOnCancelListener(new OnCancelListener() {
@Override
public void onCancel() {
}
})
.setOnProgressListener(new OnProgressListener() {
@Override
public void onProgress(Progress progress) {
}
})
.start(new OnDownloadListener() {
@Override
public void onDownloadComplete() {
}
@Override
public void onError(Error error) {
}
});
- 暂停下载
PRDownloader.pause(downloadId);
- 恢复下载
PRDownloader.resume(downloadId);
- 取消下载
// Cancel with the download id
PRDownloader.cancel(downloadId);
// The tag can be set to any request and then can be used to cancel the request
PRDownloader.cancel(TAG);
// Cancel all the requests
PRDownloader.cancelAll();
- 获取下载状态
Status status = PRDownloader.getStatus(downloadId);
- 清楚下载
PRDownloader.cleanUp(days);
更多使用方法请参考其GitHub。