app级的build.gradle导入:
implementation'cn.bingoogolapple:bga-progressbar:1.0.0@aar'
implementation"com.android.support:cardview-v7:27.1.1"
implementation'cn.bingoogolapple:bga-update:1.0.2@aar'
Activity中:
private DownloadingDialogmDownloadingDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
BGAUpgradeUtil.getDownloadProgressEventObservable().subscribe(downloadProgressEvent -> {
if (mDownloadingDialog !=null &&mDownloadingDialog.isShowing() && downloadProgressEvent.isNotDownloadFinished()) {
mDownloadingDialog.setProgress(downloadProgressEvent.getProgress(), downloadProgressEvent.getTotal());
}
});
}
/**
* 下载新版 apk 文件
*/
public void downloadApkFile(String mApkUrl,String mNewVersion) {
// 如果新版 apk 文件已经下载过了,直接 return,此时不需要开发者调用安装 apk 文件的方法,在 isApkFileDownloaded 里已经调用了安装」
if (BGAUpgradeUtil.isApkFileDownloaded(mNewVersion)) {
return;
}
// 下载新版 apk 文件
BGAUpgradeUtil.downloadApkFile(mApkUrl, mNewVersion).subscribe(new Subscriber() {
@Override
public void onStart() {
showDownloadingDialog();
}
@Override
public void onCompleted() {
dismissDownloadingDialog();
}
@Override
public void onError(Throwable e) {
dismissDownloadingDialog();
ToastUtils.showShort(SplashActivity.this,"网络异常,请稍后重试");
}
@Override
public void onNext(File apkFile) {
if (apkFile !=null) {
BGAUpgradeUtil.installApk(apkFile);
}
}
});
}
/**
* 显示下载对话框
*/
private void showDownloadingDialog() {
if (mDownloadingDialog ==null) {
mDownloadingDialog =new DownloadingDialog(this);
}
mDownloadingDialog.show();
}
/**
* 隐藏下载对话框
*/
private void dismissDownloadingDialog() {
if (mDownloadingDialog !=null) {
mDownloadingDialog.dismiss();
}
}
最后在是否更新弹框的确定按钮点击事件下:
downloadApkFile(versionBean.getData().getUrl(),versionBean.getData().getVersion());