文章分类 Classification
h5+ app升级示例
稿件来源: 互联网 撰稿作者: 匿名 发表日期: 2015-03-31 阅读次数: 507 查看权限: 游客查看
h5+ app升级示例
function CheckUpdate() { //API的GET请求地址 var CheckUrl = "http://app.xxx.com/CheckUpdate?appkey=" + APPKey + "&clientversion="+ plus.runtime.version; var RequestResponse = new Object(); RequestResponse.Success = function(Result) { var ResultObject = JSON.parse(Result); if (ResultObject.apicode == 0) { //当api返回code为0表示成功 if (ResultObject.needupdate) { ConfirmBox("有新版本,是否更新?", function() { document.getElementById("Progress_Button").style.cssText = "display: block;"; console.log(document.getElementById("Progress_Button").style.cssText); UpdateKey = ResultObject.updatekey; DownLoadFile(serverHost + "/app/update.wgt"); }, function() { return; }); } else { MessageBox("不需要更新", function() {}); } } else { //error } } RequestResponse.Error = function(Result) {} SendData(CheckUrl, RequestResponse); //发送POST } //完成更新 function FinishUpdate() { //API的GET请求地址 var UpdateUrl = "http://app.xxx.com/REST/FinishUpdate?updatekey=" + UpdateKey; UpdateUrl = UpdateUrl + "&model=" + encodeURIComponent(GetDeviceInfo().Model); UpdateUrl = UpdateUrl + "&vendor=" + encodeURIComponent(GetDeviceInfo().Vendor); UpdateUrl = UpdateUrl + "&uuid=" + encodeURIComponent(GetDeviceInfo().UUID); UpdateUrl = UpdateUrl + "&screen=" + encodeURIComponent(GetDeviceInfo().Screen); UpdateUrl = UpdateUrl + "&dpi=" + encodeURIComponent(GetDeviceInfo().DPI); UpdateUrl = UpdateUrl + "&networkinfo=" + encodeURIComponent(GetDeviceInfo().NetworkInfo); UpdateUrl = UpdateUrl + "&oslanguage=" + encodeURIComponent(GetDeviceInfo().OS.Language); UpdateUrl = UpdateUrl + "&osversion=" + encodeURIComponent(GetDeviceInfo().OS.Version); UpdateUrl = UpdateUrl + "&osname=" + encodeURIComponent(GetDeviceInfo().OS.Name); UpdateUrl = UpdateUrl + "&osvendor=" + encodeURIComponent(GetDeviceInfo().OS.Vendor); var RequestResponse = new Object(); RequestResponse.Success = function(Result) { var ResultObject = JSON.parse(Result); if (ResultObject.apicode == 0) { //当api返回code为0表示成功 } else { //ShowError(); } } RequestResponse.Error = function(Result) {} SendData(UpdateUrl, RequestResponse); //发送POST } //下载 function DownLoadFile(url) { var d = plus.downloader.createDownload(url, {}, function(f, s) { document.getElementById("Progress_Button").style.cssText = "display: none;"; ConfirmBox("下载完成,是否立即更新", function() { plus.runtime.install(f.filename, {force:true}, function() { alert("更新完毕,将重启应用!"); FinishUpdate(); plus.runtime.restart(); },function(err){ alert(JSON.stringify(err)); mui.toast("安装升级失败"); }); }, function() { return; }); }, function() { MessageBox("下载失败", function() {}); }); d.addEventListener('statechanged', function(download, status) { if (download.state == 3 && status == 200) { var percent = Math.round((download.downloadedSize / download.totalSize) * 100); document.getElementById("Progress_Button").innerHTML = (percent + "%"); } else if (download.state == 4) {} }, false); d.start(); } //确认消息 function ConfirmBox(MSG, OKFN, CancelFN) { plus.nativeUI.confirm(MSG, function(e) { if (e.index == 0) { OKFN(); } else { CancelFN(); } }, "提示", ["确定", "取消"]); } //发送数据 function SendData(URL, ResponseObject) { var MyXMLHttpRequest = new plus.net.XMLHttpRequest(); MyXMLHttpRequest.onreadystatechange = function() { switch (MyXMLHttpRequest.readyState) { case 0: break; case 1: break; case 2: break; case 3: break; case 4: if (MyXMLHttpRequest.status == 200) { ResponseObject.Success(MyXMLHttpRequest.responseText); } else { plus.nativeUI.toast("检查更新出错"); } break; } } MyXMLHttpRequest.open("GET", URL); MyXMLHttpRequest.send(); } //获得系统信息 function GetDeviceInfo() { var device = { IMEI: plus.device.imei, IMSI: "", Model: plus.device.model, Vendor: plus.device.vendor, UUID: plus.device.uuid, Screen: plus.screen.resolutionWidth * plus.screen.scale + " x " + plus.screen.resolutionHeight * plus.screen.scale + "", DPI: plus.screen.dpiX + " x " + plus.screen.dpiY, OS: new Object() }; for (var i = 0; i < plus.device.imsi.length; i++) { device.IMSI += plus.device.imsi[i]; } var types = {}; types[plus.networkinfo.CONNECTION_UNKNOW] = "未知"; types[plus.networkinfo.CONNECTION_NONE] = "未连接网络"; types[plus.networkinfo.CONNECTION_ETHERNET] = "有线网络"; types[plus.networkinfo.CONNECTION_WIFI] = "WiFi网络"; types[plus.networkinfo.CONNECTION_CELL2G] = "2G蜂窝网络"; types[plus.networkinfo.CONNECTION_CELL3G] = "3G蜂窝网络"; types[plus.networkinfo.CONNECTION_CELL4G] = "4G蜂窝网络"; device.NetworkInfo = types[plus.networkinfo.getCurrentType()]; device.OS = { Language: plus.os.language, Version: plus.os.version, Name: plus.os.name, Vendor: plus.os.vendor }; return device; }
关键词: html5puls,HBuilder 编辑时间: 2015-03-31 20:15:51
0
高兴1
支持0
搞笑0
不解0
谎言0
枪稿0
震惊0
无奈0
无聊0
反对0
愤怒
0%(0)
100%(1)
- 中搜索:h5+ app升级示例
- 中搜索:h5+ app升级示例
- 暂无评论
文章图片 article Pictrue
网友评论