HBuilder打包成apk之后,如何让版本检测自动更新
var btn = [“确定升级”, “取消”]; //获取app系统更新[是否手动点击获取更新] function appUpdate(ismanual) { console.log('appUpdate'); mui.plusReady(function () { plus.runtime.getProperty(plus.runtime.appid, function (inf) { ver = inf.version; console.log('ver:' + ver); var url = config.GetAppVersion; var client; var ua = navigator.userAgent.toLowerCase(); if (/iphone|ipad|ipod/.test(ua)) { //苹果手机 mui.ajax({ type: "get", dataType: 'json', url: "https://itunes.apple.com/lookup?id=1318127518",//获取当前上架APPStore版本信息 data: { id: 131812xxxx //APP唯一标识ID }, contentType: 'application/x-www-form-urlencoded;charset=UTF-8', success: function (data) { console.log('data:' + JSON.stringify(data)); var resultCount = data.resultCount; for (var i = 0; i < resultCount; i++) { var normItem = data.results[i].version; console.log('normItem:' + normItem) if (normItem > ver) { var _msg = "发现新版本:V" + normItem; //plus.nativeUI.alert("发现新版本:V" + normItem); mui.confirm(_msg, '升级确认', btn, function (e) { if (e.index == 0) { //执行升级操作 document.location.href = 'https://itunes.apple.com/cn/app/san-gu-hui/id131812xxxx?mt=8'; //上新APPStore下载地址 } }); return; } } if (ismanual) { mui.toast('当前版本号已是最新'); } return; } }); } else if (/android/.test(ua)) { mui.ajax(url, { data: { apkVersion: ver, }, dataType: 'json', type: 'get', timeout: 10000, success: function (data) { //console.log('data:'+JSON.stringify(data)) if (data.StatusCode = 200 && data.Data > ver) { //mui.toast("发现新版本:V" + data.Data);//获取远程数据库中上新andriod版本号 var _msg="发现新版本:V" + data.Data; mui.confirm(_msg, '升级确认', btn, function (e) { if (e.index == 0) { //执行升级操作 plus.nativeUI.toast("正在准备环境,请稍后!"); var dtask = plus.downloader.createDownload(config.apkUrl, {}, function (d, status) { if (status == 200) { var path = d.filename;//下载apk plus.runtime.install(path); // 自动安装apk文件 } else { plus.nativeUI.alert('版本更新失败:' + status); } }); dtask.start(); } }); } else { console.log('当前版本号已是最新'); if (ismanual) { mui.toast('当前版本号已是最新'); } return; } }, error: function (xhr, type, errerThrown) { if (ismanual) { mui.toast('网络异常,请稍候再试'); } } }); } }); }); } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 感谢分享 我们的ios应用是发布在苹果应用商店的,而android应用则直接部署在我们自己的服务器上面(如IIS服务器),因为android的应用市场是在太多了,那样的话每次升级版本都是一件非常麻烦的事情,当你每发布一个版本,你不得不去所有的android应用市场去提交更新。 需要注意的是:采用手动更新和自动更新调用此方法,要传入不同的参数,因为自动更新的话,如果系统检测到当前已经是最新版本,那么将不会在客户端进行展示,而手动更新的话如果已经是最新版本,那么是需要提示客户的。 自动更新调用:appUpdate();//检测app更新 手动更新调用:appUpdate(true);//检测app更新 不过我本人使用的时候,精简了一些,因为他要调取plus对象 var first = null; document.addEventListener('plusready', function() { var webview = plus.webview.currentWebview(); plus.key.addEventListener('backbutton', function() { webview.canBack(function(e) { if(e.canBack) { webview.back(); } else { if(!first){ first = new Date().getTime(); mui.toast('再按一次退出应用'); setTimeout(function(){ first = null; },1000); }else{ if(new Date().getTime()-first<1000){ plus.runtime.quit(); } } } }) }); var btn = ["确定升级", "取消"]; plus.runtime.getProperty(plus.runtime.appid, function (inf) { ver = inf.version; $.ajax({ url:"http://218.4.57.4:19011/version", success: function (data) { if (data.result.version != ver) { var _msg="发现新版本:V" + data.result.version; mui.confirm(_msg, '升级确认', btn, function (e) { if (e.index == 0) { //执行升级操作 plus.nativeUI.toast("正在准备环境,请稍后!"); var dtask = plus.downloader.createDownload('http://218.4.57.4:19011/app/xieguan.apk', { }, function (d, status) { if (status == 200) { var path = d.filename;//下载apk plus.runtime.install(path); // 自动安装apk文件 } else { plus.nativeUI.alert('版本更新失败:' + status); } }); dtask.start(); } }); } else { console.log('当前版本号已是最新'); if (ismanual) { mui.toast('当前版本号已是最新'); } return; } }, error: function (xhr, type, errerThrown) { if (ismanual) { mui.toast('网络异常,请稍候再试'); } } }); }); }); 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 还有一篇大佬差不多的文章的,也可以看看http://www.cnblogs.com/jiekzou/p/8204469.html 原文发布时间:2018-6-19 原文作者:没有名字的昵称 本文来源掘金如需转载请紧急联系作者