Ionic android 端自动更新重点记录
- 安装相应包
1
2
3
4
5
6
7
8ionic cordova plugin add cordova-plugin-file-opener2
npm install @ionic-native/file-opener
ionic cordova plugin add cordova-plugin-file-transfer
npm install @ionic-native/file-transfer
ionic cordova plugin add cordova-plugin-file-opener2
npm install @ionic-native/file-opener
- npm包版本应用4.19.0测试
- 分别引入到app.modules.ts的providers 里面
更新代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69updateApk() {
let alert1 = this.alertCtrl.create({
title: '版本更新',
message: '检查到最新版本,是否进行更新',
buttons: [
{
text: '否',
role: 'cancel',
handler: () => {
console.log('不进行更新');
super.showToast(this.toastCtrl, "不进行更新")
}
},
{
text: '是',
handler: () => {
// const updateUrl = 'http://192.168.10.112:9090/appversion.xml';
// console.log('下载地址');
// console.log(updateUrl);
// this.appUpdate.checkAppUpdate(updateUrl).then(() => { console.log('Update available') });
// alert1.dismiss();
console.log('更新APP');
let url = "http://192.168.10.112:9090/apk2.apk";
console.log(url);
console.log('开始下载Android代码----------------------------');
const fileTransfer: FileTransferObject = this.transfer.create();
try {
} catch (error) {
console.log('fileTransfer--Err');
console.log(JSON.stringify(error));
}
fileTransfer.onProgress(progressEvent => {
var present = new Number((progressEvent.loaded / progressEvent.total) * 100);
console.log('当前进度为:' + present.toFixed(0));
// var presentInt = present.toFixed(0);
// this.loadingService.presentProgress(presentInt);
});
let savePath = this.file.dataDirectory + "upload";
// var savePath = down+ 'android.apk';
console.log('保存地址');
console.log(savePath);
fileTransfer.download(encodeURI(url), savePath).then((entry) => {
super.showToast(this.toastCtrl, "下载成功");
console.log('保存apk包的地址为: ' + savePath + 'android.apk');
console.log('download complete: ' + entry.toURL());
console.log("下载成功");
this.fileOpener.open(entry.toURL(), "application/vnd.android.package-archive")
.then(() => console.log('打开apk包成功!'))
.catch(e => console.log('打开apk包失败!', e));
}, (error) => {
super.showToast(this.toastCtrl, "下载失败");
console.log("下载失败");
// this.loadingService.presentTip('操作提醒', '由于部分手机出现异常,请您进入手机设置-应用管理-Ceshiname-权限,将存储权限打开后再进行升级,由此给您带来的不便,敬请谅解。');
for (var item in error) {
console.log(item + ":" + error[item]);
}
})
}
}],
})
alert1.present();
}