uniapp打包安卓APP实现自动更新(更新app)

先列出需要注意的地方,避免新手朋友们出错

  1. 如果uniapp运行app报错:ReferenceError: plus is not defined ,普通浏览器里没有plus环境,只有HBuilder真机运行、打包后、或流应用环境下才能运行plus api。
  2. 注意url,此功能需要后端配合,具体需要什么你们商量去,(放张图,对新手友好)
  3. version建议使用整数,版本号递增
  4. 如果APPID相同,软件会自动覆盖(所以需要让更新的appid和原来的appid相同,否则就是两个程序了)
// 检查版本更新
			let _this = this
             // 获取manifest.json里的配置信息
			plus.runtime.getProperty(plus.runtime.appid, function(appInfo) { 
				_this.globalData.versionCode = appInfo.versionCode
				_this.AndroidCheckUpdate()
			}    
 AndroidCheckUpdate:function(){  
                var _this=this;  
                //这个接口让后端返回新版本的信息
                uni.request({  
                    url: 'http://xxxx.com/version.txt',  
                    method: 'GET',  
                    data: {},  
                    success: res => {
                         // 如果版本号不相同,就下载新版的应用,有的写法是res.data.data.versionCode > this.globalData.versionCode,还是看自己需求吧
                        if (res.data.data.versionCode != this.globalData.versionCode) {
                            if(plus.networkinfo.getCurrentType()!=3){  
                              uni.showModal({
								    content:'有新的版本发布,检测到您目前非Wifi连接,为节约您的流量,程序已停止自动更新,将在您连接WIFI之后重新检测更新。',
									showCancel:false,
								    success: function (res) {
								        if (res.confirm) {
								            console.log('用户点击确定');
								        }
								    }
								});
                                return;  
                            }  
                           uni.showModal({
							    content:'有新的版本发布,检测到您目前为Wifi连接,程序已启动自动更新。新版本下载完成后将自动弹出安装程序。',
								showCancel:false,
							    success: function (res) {
							        if (res.confirm) {
							            console.log('用户点击确定');
							        }
							    }
							});
                            //downloadUrl接口返回的应用下载地址
                            var dtask = plus.downloader.createDownload( res.data.data.downloadUrl, {}, function ( d, status ) {  
                                    // 下载完成  
                                    if ( status == 200 ) {   
                                        plus.runtime.install(plus.io.convertLocalFileSystemURL(d.filename),{},{},function(error){  
                                            uni.showToast({  
                                                title: '安装失败',  
                                                mask: false,  
                                                duration: 1500  
                                            });  
                                        })  
                                    } else {  
                                         uni.showToast({  
                                            title: '更新失败',  
                                            mask: false,  
                                            duration: 1500  
                                         });  
                                    }    
                                });  
                            dtask.start();   
                        }  
                    },  
                    fail: () => {},  
                    complete: () => {}  
                });  
            },  

 

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值