基于RxJava打造的下载工具

今天分享一个 下载工具:RxDownload。https://github.com/ssseasonnn/RxDownload.git。基于RxJava打造的下载工具, 支持多线程下载和断点续传,使用Kotlin编写。

添加依赖

添加jitpack仓库:

maven { url 'https://jitpack.io' }

添加RxDownload依赖:

//按需加载
implementation "com.github.ssseasonnn.RxDownload:rxdownload4:1.0.9"
implementation "com.github.ssseasonnn.RxDownload:rxdownload4-manager:1.0.9"
implementation "com.github.ssseasonnn.RxDownload:rxdownload4-notification:1.0.9"
implementation "com.github.ssseasonnn.RxDownload:rxdownload4-recorder:1.0.9"

or: 
//添加RxDownload4的所有依赖
implementation "com.github.ssseasonnn:RxDownload:1.0.9"

基础用法

开始下载:

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() 

Task Manager

获取一个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()
// 使用文件...  

Task Recorder

查询单个任务:

// 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()
展开阅读全文

页面更新:2024-05-24

标签:下载工具   字段   仓库   进度   进程   加载   对象   状态   原因   代表   文件   通知   基础   数码   列表

1 2 3 4 5

上滑加载更多 ↓
推荐阅读:
友情链接:
更多:

本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828  

© CopyRight 2020-2024 All Rights Reserved. Powered By 71396.com 闽ICP备11008920号-4
闽公网安备35020302034903号

Top