Skip to content

bingoogolapple/BGAUpdate-Android

Repository files navigation

🏃BGAUpdate-Android🏃

功能介绍

  • 适配 Android 8.+ 系统
  • 检测新版 apk 文件是否已经下载过
  • RxJava1.x + Retrofit2.x 下载新版 apk 文件
  • RxBus 监听下载进度
  • 安装 apk 文件
  • 删除之前升级时下载的老的 apk 文件

TODO

  • 支持 RxJava2.x
  • MD5 校验

效果图与示例 apk

BGAUpdateDemo

点击下载 BGAUpdateDemo.apk 或扫描下面的二维码安装

BGABannerDemo apk文件二维

使用

1.添加 Gradle 依赖

Download bga-update 后面的「latestVersion」指的是左边这个 Download 徽章后面的「数字」,请自行替换。

dependencies {
    compile 'cn.bingoogolapple:bga-update:latestVersion@aar'

    // 换成己工程里依赖的 rxjava 和 retrofit 版本
    compile 'io.reactivex:rxjava:1.3.2'
    compile 'io.reactivex:rxandroid:1.2.1'
    compile 'com.squareup.retrofit2:retrofit:2.3.0'
    compile 'com.squareup.retrofit2:converter-gson:2.3.0'
    compile 'com.squareup.retrofit2:adapter-rxjava:2.3.0'
}

2.在 Activity 的 onCreate 方法中监听下载进度。demo 里引用了 rxlifecycle 这个库来防止 Activity 内存泄漏

BGAUpgradeUtil.getDownloadProgressEventObservable()
        .compose(this.<BGADownloadProgressEvent>bindToLifecycle())
        .subscribe(new Action1<BGADownloadProgressEvent>() {
            @Override
            public void call(BGADownloadProgressEvent downloadProgressEvent) {
                if (mDownloadingDialog != null && mDownloadingDialog.isShowing() && downloadProgressEvent.isNotDownloadFinished()) {
                    mDownloadingDialog.setProgress(downloadProgressEvent.getProgress(), downloadProgressEvent.getTotal());
                }
            }
        });

3.下载新版 apk 文件。注意先申请 WRITE_EXTERNAL_STORAGE 权限

@AfterPermissionGranted(RC_PERMISSION_DOWNLOAD)
public void downloadApkFile() {
    String[] perms = {Manifest.permission.WRITE_EXTERNAL_STORAGE};
    if (EasyPermissions.hasPermissions(this, perms)) {
        // 如果新版 apk 文件已经下载过了,直接 return,此时不需要开发者调用安装 apk 文件的方法,在 isApkFileDownloaded 里已经调用了安装」
        if (BGAUpgradeUtil.isApkFileDownloaded(mNewVersion)) {
            return;
        }

        // 下载新版 apk 文件
        BGAUpgradeUtil.downloadApkFile(mApkUrl, mNewVersion)
                .subscribe(new Subscriber<File>() {
                    @Override
                    public void onStart() {
                        showDownloadingDialog();
                    }

                    @Override
                    public void onCompleted() {
                        dismissDownloadingDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        dismissDownloadingDialog();
                    }

                    @Override
                    public void onNext(File apkFile) {
                        if (apkFile != null) {
                            BGAUpgradeUtil.installApk(apkFile);
                        }
                    }
                });
    } else {
        EasyPermissions.requestPermissions(this, "使用 BGAUpdateDemo 需要授权读写外部存储权限!", RC_PERMISSION_DOWNLOAD, perms);
    }
}

4.下次进入应用时删除之前升级时下载的老的 apk 文件

BGAUpgradeUtil.deleteOldApk();

代码是最好的老师,更多详细用法请查看 demo🐾

贡献者

关于我

新浪微博 个人主页 邮箱 BGA系列开源库QQ群
bingoogolapple bingoogolapple.cn bingoogolapple@gmail.com BGA_CODE_CLUB

打赏支持

如果您觉得 BGA 系列开源库帮你节省了大量的开发时间,请扫描下方的二维码随意打赏,要是能打赏个 10.24 🐵就太👍了。您的支持将鼓励我继续创作:octocat:

如果您目前正打算购买通往墙外的梯子,可以使用我的邀请码「YFQ9Q3B」购买 Lantern,双方都赠送三个月的专业版使用时间:beers:

License

Copyright 2015 bingoogolapple

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.

About

RxJava + Retrofit 下载新版 apk 文件,RxBus 监听下载进度

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages