基于RxJava打造的下载工具, 支持多线程下载和断点续传,使用Kotlin编写
Read this in other languages: 中文, English, Changelog
-
添加jitpack仓库:
maven { url 'https://jitpack.io' }
-
添加RxDownload依赖:
//按需加载 implementation "com.github.ssseasonnn.RxDownload:rxdownload4:1.1.4" implementation "com.github.ssseasonnn.RxDownload:rxdownload4-manager:1.1.4" implementation "com.github.ssseasonnn.RxDownload:rxdownload4-notification:1.1.4" implementation "com.github.ssseasonnn.RxDownload:rxdownload4-recorder:1.1.4" or: //添加RxDownload4的所有依赖 implementation "com.github.ssseasonnn:RxDownload:1.1.4"
-
开始下载:
disposable = url.download() .observeOn(AndroidSchedulers.mainThread()) .subscribeBy( onNext = { progress -> //下载进度 button.text = "${progress.downloadSizeStr()}/${progress.totalSizeStr()}" button.setProgress(progress) }, onComplete = { //下载完成 button.text = "打开" }, onError = { //下载失败 button.text = "重试" } )
-
停止下载:
disposable.dispose()
-
获取下载文件:
val file = url.file() // 或者 val file = task.file() // 使用文件...
-
删除下载的文件:
url.delete() // 或者 task.delete()
-
获取一个TaskManager对象:
val taskManager = url.manager()
-
订阅状态更新通知:
//keep this tag for dispose val tag = taskManager.subscribe { status -> // 获取下载状态 when (status) { is Normal -> {} is Started -> {} is Downloading -> {} is Paused -> {} is Completed -> {} is Failed -> {} is Deleted -> {} } }
progress可从status中获取, 当status为Failed时, 能额外从中获取throwable,代表失败的原因
-
取消状态更新订阅:
//dispose tag taskManager.dispose(tag)
-
开始下载:
taskManager.start()
-
停止下载:
taskManager.stop()
-
删除下载:
taskManager.delete()
-
获取下载文件:
val file = taskManager.file() // 使用文件...
-
查询单个任务:
// Query task with url RxDownloadRecorder.getTask("url") .observeOn(AndroidSchedulers.mainThread()) .subscribeBy { TaskEntity -> // TaskEntity }
-
查询一批任务:
// Query task with urls RxDownloadRecorder.getTaskList("url1","url2","url3") .observeOn(AndroidSchedulers.mainThread()) .subscribeBy { list -> // list of TaskEntity }
-
获取所有下载记录列表:
RxDownloadRecorder.getAllTask() .observeOn(AndroidSchedulers.mainThread()) .subscribeBy { list -> //list of TaskEntity }
-
查询某个状态的所有下载记录:
// 查询所有下载完成的记录 RxDownloadRecorder.getAllTaskWithStatus(Completed()) .observeOn(AndroidSchedulers.mainThread()) .subscribeBy { list -> //list of TaskEntity }
-
分页查询下载记录列表:
RxDownloadRecorder.getTaskList(page, pageSize) .observeOn(AndroidSchedulers.mainThread()) .subscribeBy { list -> //list of TaskEntity }
-
分页查询某个状态下的下载记录列表:
// 获取下载完成的分页列表 RxDownloadRecorder.getTaskListWithStatus(Completed(), page, pageSize) .observeOn(AndroidSchedulers.mainThread()) .subscribeBy { list -> //list of TaskEntity }
TaskEntity 拥有一个abnormalExit字段, 该字段用来表示该Task是否是被APP强制杀进程导致的暂停
-
全部开始:
RxDownloadRecorder.startAll()
-
全部暂停:
RxDownloadRecorder.stopAll()
-
全部删除:
RxDownloadRecorder.deleteAll()
Copyright 2019 Season.Zlc 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.