HBuilder的app自動更新


hbuilder自動跟新思路:首先獲取到當前版本號,對比需要更新版本號,是否一致;否:開始更新

var wgtVer = null;

function plusReady() {
// Android處理返回鍵
plus.key.addEventListener('backbutton', function() {
if(confirm('確認退出?')) {
plus.runtime.quit();
}
}, false);

// 獲取本地應用資源版本號
plus.runtime.getProperty(plus.runtime.appid, function(inf) {
wgtVer = inf.version;
console.log("當前應用版本:" + wgtVer);
checkUpdate();
});
}
if(window.plus) {
plusReady();
} else {
document.addEventListener('plusready', plusReady, false);
}

var checkUrl = "1.0.5"; //版本號從服務器獲取
function checkUpdate() {
plus.nativeUI.showWaiting("檢測更新版本...");
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
switch(xhr.readyState) {

//4完成
case 4:
plus.nativeUI.closeWaiting();
if(xhr.status == 200) {200//ok  
console.log("檢測更新成功:" + xhr.responseText);
var newVer = checkUrl; //xhr.responseText
if(wgtVer && newVer && (wgtVer != newVer)) {
downWgt();
} else {
plus.nativeUI.alert("已是最新版,無需更新!");
}
} else {
console.log("檢測失敗!" + xhr.status + "。。。" + xhr.readyState);
plus.nativeUI.alert("檢測失敗!");
}
break;
default:
break;
}
}
xhr.open('GET', checkUrl);
xhr.send();
}

 

//下載

var Url = hturl + "APK/H5EC81A1A_HBGJ.apk"; /*下載頁,資源路徑*/
function downWgt() {
var cfi = confirm("是否下載新的安裝包?");
if(cfi == true) {
plus.nativeUI.showWaiting("正在更新軟件,請稍后...");
var dtask = plus.downloader.createDownload(Url, {}, function(d, status) { /*wode.html可以默認不用改,留着就行了*/
console.log(Url)
if(status == 200) {
console.log("成功:" + d.filename);
open(d.filename); // 打開下載地址
installWgt(d.filename);
} else {
console.log("打開下載頁面失敗!");
plus.nativeUI.alert("打開下載頁面失敗!");
}
plus.nativeUI.closeWaiting();
});

//下載事件監控
dtask.addEventListener("statechanged", function(task, status) {
if(!dtask) {
return;
}
switch(task.state) {
case 0:
console.log("未初始化!");
break;
case 1:
console.log("開始下載!");
break;
case 2:
console.log("連接到服務器!");
break;
case 3:
console.log("接收數據!");
break;
case 4:
console.log("下載完成!");
break;
}
});
dtask.start();
}
else {

}
}

//安裝

function installWgt(path) {
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.downloader.createDownload官網講解:http://www.html5plus.org/doc/zh_cn/downloader.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM