var wgtVer = null;
plus.runtime.getProperty(plus.runtime.appid, function(inf) { wgtVer = inf.version; console.log("当前应用版本:" + wgtVer); // 检测更新 var checkUrl = getAbsUrl("update/check"); function checkUpdate() { plus.nativeUI.showWaiting("检测更新..."); var xhr = new XMLHttpRequest(); xhr.onreadystatechange = function() { switch(xhr.readyState) { case 4: plus.nativeUI.closeWaiting(); if(xhr.status == 200) { console.log("检测更新成功:" + xhr.responseText); var newVer = xhr.responseText; if(wgtVer && newVer && (wgtVer != newVer)) { // 下载升级包 newVer = newVer.replace('.', ''); var wgtUrl = getAbsUrl("qr/" + newVer + ".wgt"); plus.nativeUI.showWaiting("下载wgt文件..."); plus.downloader.createDownload(wgtUrl, { filename: "_doc/update/" }, function(d, status) { if(status == 200) { console.log("下载wgt成功:" + d.filename); // 安装wgt包 var path = d.filename; plus.nativeUI.showWaiting("安装wgt文件..."); plus.runtime.install(path, {}, function() { plus.nativeUI.closeWaiting(); console.log("安装wgt文件成功!"); plus.nativeUI.alert("应用资源更新完成!", function() { plus.runtime.restart(); }); }, function(e) { plus.nativeUI.closeWaiting(); console.log("安装wgt文件失败[" + e.code + "]:" + e.message); plus.nativeUI.alert("安装wgt文件失败[" + e.code + "]:" + e.message); }); } plus.nativeUI.closeWaiting(); }).start(); } } else { console.log("检测更新失败!"); } break; default: break; } } xhr.open('POST', checkUrl); xhr.send(); } checkUpdate(); });
下面是整包更新的代码:
mui.getJSON(getAbsUrl('update/check'), { "appid": plus.runtime.appid, "version": plus.runtime.version }, function(data) { if(data.status) { plus.nativeUI.confirm(data.note, function(event) { if(0 == event.index) { plus.runtime.openURL(data.url); } }, data.title, ["立即更新", "取 消"]); } });