引入库
compile 'com.allenliu.versionchecklib:library:2.0.0'
//用于标记是否正在版本更新
private static boolean upAppFirst = true;
联网获取服务器版本信息
//版本更新
private void checkVersion() {
if (upAppFirst) {
httpManager.doHttpDeal(new CheackVersionApi());//网络请求
} else {
ToastUtils.show(getActivity(), "正在进行版本更新...");
}
}
在获取数据成功的时候,获取本地版本信息,判断是否需要进行更新,这里就不用写了
if (isNeedUpdate) {
if (!TextUtils.isEmpty(resultBean.getDownloadUrl())) {
builder = AllenVersionChecker
.getInstance()
.requestVersion()
.setRequestUrl("https://www.baidu.com")
.request(new RequestVersionListener() {
@Nullable
@Override
public UIData onRequestVersionSuccess(String result) {
UIData uiData = UIData.create();
uiData.setTitle(getString(R.string.app_name));
uiData.setDownloadUrl(resultBean.getDownloadUrl());
uiData.setContent(getString(R.string.updatecontent));
return uiData;
}
@Override
public void onRequestVersionFailure(String message) {
Toast.makeText(getActivity(), "request failed", Toast.LENGTH_SHORT).show();
upAppFirst = true;
ToastUtils.show(getActivity(), "下载地址不可用,请联系客服");
}
});
builder.setShowDownloadingDialog(false);
builder.setNotificationBuilder(createCustomNotification());
builder.setDownloadAPKPath(Environment.getExternalStorageDirectory().getAbsolutePath() + "/lifeCrys/");
builder.setApkDownloadListener(new APKDownloadListener() {
@Override
public void onDownloading(int progress) {
if (upAppFirst) {
upAppFirst = false;
ToastUtils.show(getActivity(), "正在更新...");
}
}
@Override
public void onDownloadSuccess(File file) {
upAppFirst = true;
}
@Override
public void onDownloadFail() {
upAppFirst = true;
ToastUtils.show(getActivity(), "下载地址不可用,请联系客服");
}
});
builder.excuteMission(getActivity());
} else {
ToastUtils.show(getActivity(), "下载地址不可用,请联系客服");
}
} else {
ToastUtils.show(getActivity(), "当前版本已经是最新的版本了");
}
}
通知栏显示状态
private NotificationBuilder createCustomNotification() {
return NotificationBuilder.create()
.setRingtone(true)
.setIcon(R.mipmap.logo)
.setTicker("custom_ticker")
.setContentTitle(getString(R.string.update))
.setContentText(getString(R.string.custom_content_text));
}