uniapp 在线更新应用

在线更新应用及进度条显示

1.比较现安装手机中的apk 与线上apk的版本

javascript 复制代码
getVersion(){
				var newVersion=uni.getStorageSync("newVersion").split(".")
				var version=plus.runtime.version.split(".") // 获取手机安装的版本
				var versionNum="",newVersionNum=""
				for(var i=0;i<version.length;i++){
					versionNum+=version[i]
				}
				for(var i=0;i<newVersion.length;i++){
					newVersionNum+=newVersion[i]
				}
				if(versionNum<newVersionNum){
					this.chooseUpApp()
				}
			},

2.选择android的方式安装还是apple的testFligt方式安装

javascript 复制代码
chooseUpApp(){
				uni.showModal({
				    title: '提示',
				    content: plus.os.name == 'Android'?'检测到有最新版本,是否下载!':'检测到有最新版本,是否去TestFlight下载!',
				    success:(res)=> {
				        if (res.confirm) {
							if(plus.os.name == 'Android'){
								this.isUpApp=true
								this.upApp()
							}else{
								plus.runtime.launchApplication({ action:this.upUrl}, function(e) {
									uni.showToast({
										icon:"none",
										title:"请确认手机安装了TestFlight"
									})
									console.log('Open system default browser failed: ' + e.message);  
								});  
							}
				        } else if (res.cancel) {
				            console.log('用户点击取消');
				        }
				    }
				});
			},

3.安装apk,并显示进度条

javascript 复制代码
upApp(){//下载APP
				var downloadTask=uni.downloadFile({
					url:this.upUrl,
					complete(res){
						console.log(res)
						if(res.statusCode==200){
							plus.runtime.install(res.tempFilePath, {
								force: false
							}, (e) => {
								this.isUpApp=false
								plus.runtime.restart();
							}, (e) => {
								console.log(e);
								this.isUpApp=false
								uni.showToast({
									title: '安装升级包失败',
									icon: 'none'
								})
							});
						}
					}
				})
				this.progress=0
				// console.log(downloadTask)
				downloadTask.onProgressUpdate((res) => { //监听下载进度变化
					console.log(res)
					if(this.progress!=res.progress){
						this.progress=res.progress
						// console.log('下载进度' + res.progress);
						// console.log('已经下载的数据长度' + res.totalBytesWritten);
						// console.log('预期需要下载的数据总长度' + res.totalBytesExpectedToWrite);
					}
				    // 测试条件,取消下载任务。
				});
			},
相关推荐
朱涛的自习室12 小时前
70 美金的 Beyond Compare,我让 Gemini 4 和 DeepSeek 造个开源免费版(Day 2)
android·前端·人工智能
.Bryce.17 小时前
在智能体中使用A2A工具:让大模型自己决定“该找谁帮忙“
android·java·服务器·redis·后端·agent·a2a
方舟小孩18 小时前
开源办公软件海豹办公 1.9.15 实战 从模板异常到 Android WebView 资源回归
android
用户21816970493018 小时前
Kotlin 语法四 协程
android
出师牛18 小时前
Android - 再探Handler
android
高晶18 小时前
Android 物联网组网实战:NFC 从零到落地
android·java
stevenzqzq18 小时前
android 维度和渠道
android
古法安卓18 小时前
Android-SystemUI ANR协程相关问题排查
android·java·android studio
xiaogai_gai19 小时前
生产汇报单查询同步实战:从金蝶云星空拉取到轻易云数据集成平台的工程化做法
android
释厄62320 小时前
错误无误公理——错误正确本同源都迭代定律——逆转有价值
android·开发语言·kotlin