- 适配 Android 8.+ 系统
- 检测新版 apk 文件是否已经下载过
- RxJava1.x + Retrofit2.x 下载新版 apk 文件
- RxBus 监听下载进度
- 安装 apk 文件
- 删除之前升级时下载的老的 apk 文件
- 支持 RxJava2.x
- MD5 校验
点击下载 BGAUpdateDemo.apk 或扫描下面的二维码安装
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'
}
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());
}
}
});
@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);
}
}
BGAUpgradeUtil.deleteOldApk();
代码是最好的老师,更多详细用法请查看 demo🐾
新浪微博 | 个人主页 | 邮箱 | BGA系列开源库QQ群 |
---|---|---|---|
bingoogolapple | bingoogolapple.cn | [email protected] |
如果您觉得 BGA 系列开源库帮你节省了大量的开发时间,请扫描下方的二维码随意打赏,要是能打赏个 10.24 🐵就太👍了。您的支持将鼓励我继续创作
如果您目前正打算购买通往墙外的梯子,可以使用我的邀请码「YFQ9Q3B」购买 Lantern,双方都赠送三个月的专业版使用时间:beers:
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.