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);
					}
				    // 测试条件,取消下载任务。
				});
			},
相关推荐
似水明俊德1 小时前
15-C#
android·开发语言·c#
阿拉斯攀登1 小时前
第 19 篇 驱动性能优化与功耗优化实战
android·驱动开发·瑞芯微·嵌入式驱动·安卓驱动
91刘仁德2 小时前
C++ 内存管理
android·c语言·数据结构·c++·经验分享·笔记·算法
小强开学前2 小时前
自定义 Drawable 实现任意高度纯圆角背景及玻璃效果
android
秃了也弱了。3 小时前
ElasticSearch:优化案例实战解析(持续更新)
android·java·elasticsearch
恋猫de小郭3 小时前
Kotlin 在 2.0 - 2.3 都更新了什么特性,一口气带你看完这两年 Kotlin 更新
android·前端·flutter
墨狂之逸才4 小时前
React Native 移动项目目录导致的 Android 编译失败问题及解决方案
android·react native
feng一样的男子4 小时前
住在手机里的“小龙虾” (OpenClaw):接入本地模型,解决记忆“装死”顽疾
android·ai·智能手机·openclaw
hongtianzai4 小时前
MySQL中between and的基本用法
android·数据库·mysql
Zender Han5 小时前
Flutter Bloc / Cubit 最新详解与实战指南(2026版)
android·flutter·ios