<script type="text/javascript">
function plusReady(){
plus.key.addEventListener('backbutton',function(){
if(confirm('确认退出?')){
plus.runtime.quit();
}
},false);
var checkUrl="http://xxxxxx/version.txt";
plus.runtime.getProperty(plus.runtime.appid,function(inf){
var wgtVer = inf.version;
console.log("当前应用版本:"+wgtVer);
var xhr=new XMLHttpRequest();
xhr.onreadystatechange=function(){
switch(xhr.readyState){
case 4:
plus.nativeUI.closeWaiting();
if(xhr.status==200){
var newVer=xhr.responseText;
console.log("检测更新成功:"+newVer);
if(newVer.toString().substr(0,5) == wgtVer.toString().substr(0,5)){
console.log("无新版本可更新!")
// plus.nativeUI.alert("无新版本可更新!");
}else{
downWgt(); // 下载升级包
console.log("正在更新")
}
}else{
console.log("检测更新失败!");
plus.nativeUI.alert("检测更新失败!");
}
break;
default:
break;
}
}
xhr.open('GET',checkUrl);
xhr.send();
});
}
if(window.plus){
plusReady();
}else{
document.addEventListener('plusready',plusReady,false);
}
// 下载wgt文件
var wgtUrl="http://xxxxxx/update.wgt";
function downWgt(){
plus.nativeUI.showWaiting("下载wgt文件...");
plus.downloader.createDownload( wgtUrl, {filename:"_doc/update/"}, function(d,status){
if ( status == 200 ) {
console.log("下载wgt成功:"+d.filename);
installWgt(d.filename); // 安装wgt包
} else {
console.log("下载wgt失败!");
plus.nativeUI.alert("下载wgt失败!");
}
plus.nativeUI.closeWaiting();
}).start();
}
// 更新应用资源
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);
});
}
</script>