# PRDownloader **Repository Path**: HarmonyOS-tpc/PRDownloader ## Basic Information - **Project Name**: PRDownloader - **Description**: openHarmony的文件下载器库,支持暂停和恢复 - **Primary Language**: Unknown - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 1 - **Created**: 2021-04-01 - **Last Updated**: 2023-04-17 ## Categories & Tags **Categories**: harmonyos-network **Tags**: None ## README # PRDownloader - openHarmony的文件下载器库,支持暂停和恢复 ## 示例 ![image](https://gitee.com/openharmony-tpc/PRDownloader/blob/master/gif/prdownload.gif) ### Overview of PRDownloader library PR下载器可用于下载任何类型的文件,如图像、视频、pdf、har等。 * PR下载器可用于下载任何类型的文件,如图像、视频、pdf、apk等。 * 此文件下载程序库支持下载文件时暂停和恢复。 * 支持大文件下载。 * 此下载程序库有一个简单的界面,用于发出下载请求。 * 我们可以使用给定的下载ID检查下载状态。 * PR下载程序在下载文件时提供onProgress、onCancel、onStart、onError等所有内容的回调。 * 支持合理的请求取消。 * 许多请求可以并行提出。 * 所有类型的定制都是可能的。 ## 集成 ### 方案一 ``` 添加har包到lib文件夹内 在entry的gradle内添加如下代码 implementation fileTree(dir: 'libs', include: ['*.jar', '*.har']) ``` ### 方案二 ``` implementation project(':library') ``` ### 方案三 ``` implementation 'io.openharmony.tpc.thirdlib:PRDownloader:1.0.2' ``` 不要忘记在清单中添加internet权限(如果尚未存在) ```xml "reqPermissions": [ { "name": "ohos.permission.INTERNET" } ] ``` 然后在应用程序类的onCreate()方法中初始化它: ```java PRDownloader.initialize(getApplicationContext()); ``` 使用一些自定义来初始化它 ```java // 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); ``` ### 发起下载请求 ```java 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) { } }); ``` ### 暂停下载请求 ```java PRDownloader.pause(downloadId); ``` ### 恢复下载请求 ```java PRDownloader.resume(downloadId); ``` ### 取消下载请求 ```java // 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(); ``` ### 下载请求状态 ```java Status status = PRDownloader.getStatus(downloadId); ``` ### 如果启用了数据库,则清除恢复的文件 ```java // Method to clean up temporary resumed files which is older than the given day PRDownloader.cleanUp(days); ``` ### TODO * 与OkHttp、RxJava等其他库集成 * 测试用例 * 当然还有许多功能和错误修复 ### License ``` Copyright (C) 2017 MINDORKS NEXTGEN PRIVATE LIMITED Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ```