第三方扩展开源库挖掘&推荐

安卓文件下载库 | PRDownloader

2017-12-05  本文已影响228人  5afd372c86ba
名称 PRDownloader
语言 Android
平台 GitHub
作者 MindorksOpenSource
链接 点此进入

今天推荐的是一款具备完善功能的安卓文件下载库——PRDownloader 。

特点

使用方法

  1. 添加依赖
compile 'com.mindorks.android:prdownloader:0.2.0'
  1. 添加权限
<uses-permission android:name="android.permission.INTERNET" />
  1. 在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);
  1. 请求下载文件
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) {

                            }
                        });
  1. 暂停下载
PRDownloader.pause(downloadId);
  1. 恢复下载
PRDownloader.resume(downloadId);
  1. 取消下载
// 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();
  1. 获取下载状态
Status status = PRDownloader.getStatus(downloadId);
  1. 清楚下载
PRDownloader.cleanUp(days);

更多使用方法请参考其GitHub。

上一篇下一篇

猜你喜欢

热点阅读