1,在頁面首頁判斷是否為android,獲取manifest.json配置的版本號
onLoad() { let that = this //#ifdef APP-PLUS uni.getSystemInfo({ //獲取系統信息 success: (res) => { if (res.platform == "android") { //檢測當前平台,如果是安卓則啟動安卓更新 plus.runtime.getProperty(plus.runtime.appid, function(wgtinfo) { //獲取配置信息 if (wgtinfo.name == "NMFS-Uni") { that.version = wgtinfo.version //當前版本號 that.AndroidCheckUpdate(); } }); } } }) //#endif },
2.獲取最新的版本號,與本身版本號比較
methods: { AndroidCheckUpdate: function() { //版本更新 var _this = this;
uni.request({
url: 'http://xxxx.com/version.txt',
method: 'GET',
data: {},
success: res => {
var server_version = res.version;
_this.checkVersionToLoadUpdate(server_version, _this.version);
}
},
checkVersionToLoadUpdate: function(server_version, curr_version) { //版本更新 if (server_version > curr_version) { if (plus.networkinfo.getCurrentType() != 3) {//獲取手機設備的相關信息,判斷是否在Wifi狀態。 uni.showToast({ title: '有新的版本發布,檢測到您目前非Wifi連接,為節約您的流量,程序已停止自動更新,將在您連接WIFI之后重新檢測更新', mask: true, duration: 5000, icon: "none" }); return; } else { uni.showModal({ title: "版本更新", content: '有新的版本發布,檢測到您當前為Wifi連接,是否立即進行新版本下載?', confirmText: '立即更新', cancelText: '稍后進行', success: function(res) { if (res.confirm) { var downloadApkUrl = 'http://cwwy.caiyunduan.com/apk/mfs.apk ';//apk下載的地址 var dtask = plus.downloader.createDownload(downloadApkUrl, {},function(d, status) {//新建下載任務 if (status == 200) {//當下載完成 plus.runtime.install(plus.io.convertLocalFileSystemURL(d.filename), {}, {}, function(error) {//安裝應用 uni.showToast({ title: '安裝失敗', duration: 1500, icon: 'none' }); }) } else { uni.showToast({ title: '更新失敗', duration: 1500, icon: 'none' }); } }) dtask.start(); var prg = 0; var showLoading = plus.nativeUI.showWaiting("正在下載"); dtask.addEventListener('statechanged', function(task, status) {//添加下載任務事件監聽器 // 給下載任務設置一個監聽 並根據狀態 做操作 switch (task.state) { case 1: showLoading.setTitle("正在下載"); break; case 2: showLoading.setTitle("已連接到服務器"); break; case 3: prg = parseInt(//下載的進度 (parseFloat(task.downloadedSize) / parseFloat(task.totalSize)) * 100 ); showLoading.setTitle("版本更新,正在下載" + prg + "% "); break; case 4: plus.nativeUI.closeWaiting();//關閉系統提示框 //下載完成 break; } }); } } }) } } },
}
注意!
plus對象只有在手機端,真機調試才有。不然會報 plus is not defined