uniapp写安卓端app自动更新下载apk安装

需要后端返回一个接口,接口里有最新版本号和下载地址,通过对比当前版本号和最新版本号来控制下载和安装最新版本

复制代码
//获取当前版本
AndroidCheckUpdate() {
	const systemInfo = uni.getSystemInfoSync();
	this.version = Number(systemInfo.appVersion.split('.').join(''))
	home.getVersionNew({}).then(res => {
		console.log(res,'res')
		var versions = Number(res.data.version.split('.').join(''))||Number(version)
		if(versions>this.version){
			uni.showModal({
				title:'版本提示',
				content:'检查到当前版本不是最新版本,是否更新为最新版本?',
				success:(e)=> {
					if(e.confirm){
						console.log(e)
						this.pullapk(res.data.fileUrl)
						//下载更新
					}
				}
			})
		}
	}).catch(e => {
		uni.showToast({
			title:e.msg,
			icon:'none'
		})
		return
	})
	},

pullapk(url){
	console.log(url)
	uni.showLoading({ title: "下载中" });
	uni.downloadFile({
		url: url, //下载地址
		success: (res) => {
			console.log(res, "下载成功");
			if (res.statusCode === 200) {
				console.log("下载成功");
				uni.hideLoading();
				uni.showToast({ title: "下载成功", icon: "success" });
				uni.saveFile({
					tempFilePath: res.tempFilePath,
					success: function (res) {
						uni.openDocument({
							filePath: res.savedFilePath,
							success: function (res) {
								console.log(res, "打开安装包");
							},
						});
					},
					fail: (err) => {
						console.log(err, "打开安装包-失败");
					},
				});
			}
		},fail(e) {
			uni.hideLoading();
			uni.showToast({ title: "下载失败,请检查网络", icon: "none" });
			console.log(e)
		},
	})
},
相关推荐
We་ct13 分钟前
LeetCode 5. 最长回文子串:DP + 中心扩展
前端·javascript·算法·leetcode·typescript
liang_jy6 小时前
Android SparseArray
android·源码
liang_jy6 小时前
Activity 启动流程扩展篇(一)—— startActivityInner 任务决策全解析
android·源码
NPE~7 小时前
[App逆向]脱壳实战
android·教程·逆向·android逆向·逆向分析
木易 士心8 小时前
别再只会用 drawCircle 了!一文搞懂 Android Canvas 底层机制
android
cn_mengbei8 小时前
用React Native开发OpenHarmony应用:Reanimated共享元素过渡
javascript·react native·react.js
kyriewen8 小时前
前端测试:别为了100%覆盖率而写测试,那是自欺欺人
前端·javascript·单元测试
Data_Journal9 小时前
如何使用cURL更改User Agent
大数据·服务器·前端·javascript·数据库
掌心向暖RPA自动化9 小时前
如何获取网页某个元素在屏幕可见部分的中心坐标影刀RPA懒加载坐标定位技巧
java·javascript·自动化·rpa·影刀rpa
AtOR CUES9 小时前
MySQL——表操作及查询
android·mysql·adb