umeng提供了手动升级的api。
所以,我们只需要配置一下在线参数,然后根据这个参数是否强制更新,弹出不同的对话框即可。
代码如下:
public static void showUpdateDialog(final boolean isForce,
final UpdateResponse response,
final Activity activity) {
final VipUpdateDialog dlg = new VipUpdateDialog(activity,
new VipUpdateDialog.OnFinishListener() {
@Override
public boolean onFinish(View view) {
// update
downloadFromResponse(response, activity);
if(isForce && activity != null) {
activity.finish();
}
return true;
}
@Override
public boolean onCancel(View view) {
// exit
if(isForce && activity != null) {
activity.finish();
}
return true;
}
});
dlg.setSelfTitle("發現新版本V" + response.version);
dlg.setMessage(response.updateLog);
dlg.setCancelable(!isForce);
if(isForce){
dlg.setNagtiveButton(activity.getString(R.string.exist));
} else {
dlg.setNagtiveButton(activity.getString(R.string.cancel));
}
dlg.show();
}
/**
* 版本更新提示
* @param context 上下文
* @param isNeedTost 如果需要toast,就提示,否則,不提示
*/
public static void checkVersionUpdate(final Context context, final boolean isNeedTost) {
UmengUpdateAgent.setUpdateAutoPopup(false);
UmengUpdateAgent.setUpdateListener(new UmengUpdateListener() {
@Override
public void onUpdateReturned(int updateStatus, UpdateResponse updateInfo) {
switch (updateStatus) {
case UpdateStatus.Yes: // has update
// UmengUpdateAgent.showUpdateDialog(getActivity(), updateInfo);
umengUpdateByConfig(updateInfo, context);
break;
case UpdateStatus.No: // has no update
if (isNeedTost) {
Tosts.msgShort(context, R.string.its_lastst_version);
}
break;
case UpdateStatus.NoneWifi: // none wifi
// Toast.makeText(getActivity(), "没有wifi连接, 只在wifi下更新", Toast.LENGTH_SHORT).show();
break;
case UpdateStatus.Timeout: // time out
// Toast.makeText(getActivity(), "超时", Toast.LENGTH_SHORT).show();
break;
}
}
});
UmengUpdateAgent.update(context);
}
private static void umengUpdateByConfig(UpdateResponse resposne, Context context){
String isUpdate = MobclickAgent.getConfigParams(context, "XfthkAForceUpdate");
boolean flag = false;
if(isUpdate.equals("true")) {
flag = true;
}
showUpdateDialog(flag, resposne, (BaseActivity)context);
}
private static void downloadFromResponse(UpdateResponse response, final BaseActivity activity) {
UmengUpdateAgent.setDownloadListener(new UmengDownloadListener(){
@Override
public void OnDownloadStart() {
// Tosts.msgShort(activity, "download start");
}
@Override
public void OnDownloadUpdate(int progress) {
// Tosts.msgShort(activity, "download progress : " + progress + "%");
}
@Override
public void OnDownloadEnd(int result, String file) {
//Toast.makeText(mContext, "download result : " + result , Toast.LENGTH_SHORT).show();
// Toast.makeText(mContext, "download file path : " + file , Toast.LENGTH_SHORT).show();
UmengUpdateAgent.startInstall(activity, new File(file));
}
});
// 如果已经下载完成直接安装
File downloadFile = UmengUpdateAgent.downloadedFile(activity, response);
if(downloadFile != null){
UmengUpdateAgent.startInstall(activity, downloadFile);
} else {
UmengUpdateAgent.startDownload(activity, response);
// UmengUpdateAgent.showUpdateNotification(activity, response);
}
}
调用其中
checkVersionUpdate(final Context context, final boolean isNeedTost) 即可。
代码地址:http://download.csdn.net/detail/cdkd123/8956121
2016-8-5 补充:
umeng在线参数配置可能不是很及时。这里我们从
setUpdateListener监听回调里,获取的UpdateResponse对象是及时的。我们可以设置通过更新提示msg字段(名字可能不对)的第一个字符为0,就是强制更新。
2016-10月1日开始umeng更新就不能用了。大家赶紧改用其他第三方sdk吧。