Git Product home page Git Product logo

bgaupdate-android's Introduction

🏃BGAUpdate-Android🏃

功能介绍

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

效果图与示例 apk

BGAUpdateDemo

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

BGABannerDemo apk文件二维

使用

1.添加 Gradle 依赖

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

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

    // 换成己工程里依赖的 rxjava 和 retrofit 版本
    compile 'io.reactivex:rxjava:1.2.2'
    compile 'io.reactivex:rxandroid:1.2.1'
    compile 'com.squareup.retrofit2:retrofit:2.1.0'
    compile 'com.squareup.retrofit2:converter-gson:2.1.0'
    compile 'com.squareup.retrofit2:adapter-rxjava:2.1.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 [email protected] 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.

bgaupdate-android's People

Contributors

bingoogolapple avatar chenfei0928 avatar

Watchers

James Cloos avatar feisher avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.