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)
		},
	})
},

 

  • 8
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值