名稱 | PRDownloader |
---|---|
語(yǔ)言 | Android |
平臺(tái) | GitHub |
作者 | MindorksOpenSource |
鏈接 | 點(diǎn)此進(jìn)入 |
今天推薦的是一款具備完善功能的安卓文件下載庫(kù)——PRDownloader 匾委。
特點(diǎn)
支持下載任意文件拖叙;
支持暫停和恢復(fù)下載;
支持下載大文件赂乐;
使用簡(jiǎn)單薯鳍;
提供多個(gè)接口,支持回調(diào)開始下載挨措、下載中挖滤,下載結(jié)束,出現(xiàn)錯(cuò)誤各種情況浅役;
支持適當(dāng)?shù)南螺d請(qǐng)求取消斩松;
支持并行下載;
使用方法
- 添加依賴
compile 'com.mindorks.android:prdownloader:0.2.0'
- 添加權(quán)限
<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);
- 請(qǐng)求下載文件
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);
- 恢復(fù)下載
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();
- 獲取下載狀態(tài)
Status status = PRDownloader.getStatus(downloadId);
- 清楚下載
PRDownloader.cleanUp(days);
更多使用方法請(qǐng)參考其GitHub觉既。