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(); });
下面是整包更新的代碼:app
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, ["當即更新", "取 消"]); } });