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);
					}
				    // 测试条件,取消下载任务。
				});
			},
相关推荐
90后的晨仔1 小时前
解决 Android 构建报错:Unable to delete file .../file-map.txt
android·前端
mg6681 小时前
安卓玩机工具----安卓手机变成桌面电脑!多窗口镜像+音频投射+游戏操控,无需 Root
android·智能手机·电脑
peakmain92 小时前
异常处理 - Native 层的崩溃捕获机制及实现 (一)
android·性能优化
木头左3 小时前
【Android】听写播放器改版:中英文书写速度拆项与「正在读」高亮
android·room·media3·jetpack compose·听写
好的佩奇4 小时前
Dart 之方法、操作符与可调用对象
android·flutter·dart
恋猫de小郭4 小时前
Flutter Impeller Vulkan 内存持续上涨,问题没想到是文字阴影
android·前端·flutter
张小姐的猫15 小时前
【MySQL】访问 & 数据流动原理
android·数据库·mysql·adb·1024程序员节
朱涛的自习室17 小时前
212 个需求、22 万行代码:Agent 接管开源项目的 19 天
android·前端·人工智能
传奇开心果1 天前
【Flutter踩坑排雷】第3课:const 误用坑|const 错误使用引发 UI 异常与性能损耗
android·flutter