mui 应用程序版本更新方法

我才用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>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值