我才用wgt应用资源更新方式,速度快。
在应用程序入口index.html放置js内容
<script type="text/javascript">
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);
});
}
if(window.plus){
plusReady();
}else{
document.addEventListener('plusready',plusReady,false);
}
// 检测更新
var checkUrl="http://demo.dcloud.net.cn/test/update/check.php";
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)){
downWgt(); // 下载升级包
}else{
plus.nativeUI.alert("无新版本可更新!");
}
}else{
console.log("检测更新失败!");
plus.nativeUI.alert("检测更新失败!");
}
break;
default:
break;
}
}
xhr.open('GET',checkUrl);
xhr.send();
}
// 下载wgt文件
var wgtUrl="http://218.205.36.78:8086/H53379DE1.wgt";
function downWgt(){
plus.nativeUI.showWaiting("更新中...");
plus.downloader.createDownload( wgtUrl, {filename:"_doc/update/"}, function(d,status){
if ( status == 200 ) {
console.log("下载更新成功:"+d.filename);
installWgt(d.filename); // 安装wgt包
} else {
console.log("下载更新失败!");
plus.nativeUI.alert("下载更新失败!");
}
plus.nativeUI.closeWaiting();
}).start();
}
// 更新应用资源
function installWgt(path){
plus.nativeUI.showWaiting("安装更新文件...");
plus.runtime.install(path,{force:true},function(){
plus.nativeUI.closeWaiting();
console.log("安装更新文件成功!");
plus.nativeUI.alert("更新完成!",function(){
plus.runtime.restart();
});
},function(e){
plus.nativeUI.closeWaiting();
console.log("安装更新文件失败["+e.code+"]:"+e.message);
plus.nativeUI.alert("安装更新文件失败["+e.code+"]:"+e.message);
});
}
</script>
我在plus.runtime.install(path,{force:true},function()中放置了force:true,是因为,如果wgt的版本和当前版本相同的话会报错,就强制更新了。原因:懒
加个按钮
<button οnclick="checkUpdate()" style="position: fixed; bottom:4rem;left: 10%;margin-left:250px;">检查更新</button>
本文介绍了一种使用WGT格式的应用资源更新方法,并详细展示了如何通过JavaScript代码实现自动检测更新、下载升级包及安装更新的过程。此外,还提供了一个用于触发更新检查的按钮。
335

被折叠的 条评论
为什么被折叠?



