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);
					}
				    // 测试条件,取消下载任务。
				});
			},
相关推荐
XiaoLeisj21 分钟前
Android 权限管理实战:运行时申请、ActivityResultLauncher 与设置页授权
android·java·权限
橙子1991101627 分钟前
Android 中的权限申请
android
2501_9159214331 分钟前
iOS APP上架工具,在没有 Mac 的环境中发布苹果应用
android·macos·ios·小程序·uni-app·iphone·webview
范特西林33 分钟前
第一篇:从电源键到上帝进程——硬件觉醒与 Init 的诞生
android
养了一只皮卡丘34 分钟前
ubuntu22.04搭建mysql8.0.45 mgr (2)
android·adb
常利兵36 分钟前
深入理解Android ViewModel&SavedStateHandle:告别数据丢失,打造稳健UI架构
android·ui·架构
范特西林38 分钟前
第四篇:从点击到显示——App 启动与 Activity 生命周期全追踪
android
ke_csdn39 分钟前
安卓的视频通讯
android·音视频
范特西林40 分钟前
第二篇:Java 世界的“创世神”:Zygote 如何一秒孵化一个 App?
android
范特西林41 分钟前
第三篇:SystemServer——Android 框架层的大脑
android