RxUpDownloader

RxUpDownloader 使用RxJava2+Retrofit2+OkHttp3 以及IntentService来 下载/上传 文件。而且支持进度回调、任务状态通知(成功/失败)以及取消任务。 使用起来非常简单,适合直接拿来使用。

入门

第一步是将RxUpDownloader集成到你的项目中。

  • 因为library放在 jitpack.io上, 所以首先需要将 jitpack.io 仓库加入到你的项目根Gradle中:
allprojects {
        repositories {
            ...
            maven { url 'https://jitpack.io' }
        }
    }
  • 接下来将UpDownloader加入到项目中:
dependencies {
            ...
            implemention 'com.github.wind0ws:rxupdownloader:x.y.z'
    }

如果你的gradle版本低于3.0, 那么你需要将 implemention 关键词替换为 compile.
提示: 你应该将 x.y.z 替换为正确的版本, 你可在 发布页 找到版本号.

好了, 我们已经完成集成了。

Hello,World.

RxUpDownloader 需要在manifest.xml中添加 UpDownService , 因为这个library的原理就是将上传下载任务发给这个Service,由他在后台下载/上传。
参考示例:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.threshold.rxupdownloader">
    <!-- 需要添加 网络 和 存储权限 去上传/下载-->
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

    <application
        android:name=".App"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <!-- 这就是我们添加的service,由他来负责上传下载任务 -->
        <service android:name="com.threshold.updownloader.UpDownService"/>

        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>

</manifest>

注意: 从Android6.0开始,Android新增了运行时权限,所以仅仅在manifest.xml中声明所需的权限是不够的,你还应该在应用中申请存储权限:WRITE_EXTERNAL_STORAGE,否则在上传下载的时候由于没有权限会导致应用闪退。

现在我们应该有上传下载文件的能力了:

  • 下载文件:
 UpDownService.startServiceForDownload(context,1002,true,
                    "http://issuecdn.baidupcs.com/issue/netdisk/yunguanjia/BaiduNetdisk_5.6.3.exe")
  • 上传文件:
UpDownService.startServiceForUpload(context,1001,
                                  "http://your.restful.website/files","/sdcard/1.txt")

上面参数中的 "1001" and "1002" 是 taskId, 你可以通过
UpDownService.getTaskDisposable(taskId) 来获取这个任务的 disposable , 然后你可以通过这个disposable来取消任务.
所有的上传/下载任务都只在后台线程执行.

监听上传/下载状态:

我们使用 RxBus2 来发射/监听 进度/成功/失败 事件.

示例如下:

          RxBus.getDefault()
                .ofType(UpDownEvent::class.java)
                .filter {
                    //   it.taskId == 1002
                    it.type == UpDownEvent.Type.DOWNLOAD
                }
                .subscribeOn(Schedulers.io())
                .observeOn(Schedulers.io())
                .subscribe {
                    when (it) {
                        is UpDownProgressEvent -> {
                            info { "Current progress: ${it.percent}" }
                        }
                        is UpDownSucceedEvent -> debug { "Download succeed: $it" }
                        is UpDownFailedEvent -> debug { "Download failed: $it" }
                    }
                }

注意:不是所有的下载任务都能获取到进度值。能不能获取到下载进度值取决于你的下载服务器:如果你的服务器返回的 content-typeapplication/octet-stream, 那么就获取不到content-length的值,所以就无法计算下载进度。
默认的content-length是 -1, 所以如果你发现进度低于0,这就意味着这个进度不是真实的进度,他的绝对值实际上是目前已下载的数据的大小(单位:字节)
通常来说,进度值范围为0~100
UpDownSucceedEvent UpDownFailedEvent 事件是一直可靠的.

  • UpDownSucceedEvent 对应 下载/上传 成功事件.
  • UpDownFailedEvent 对应 下载/上传 失败事件.

Want demo?

看看这个项目的 app 模块。

关注我的公众号.jpg
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容