1、安装File,用于文件目录读取
ionic cordova plugin add cordova-plugin-file npm install --save @ionic-native/file
2、安装Version,,用于获取当前app版本
ionic cordova plugin add cordova-plugin-app-version npm install --save @ionic-native/app-version
3、安装transfer插件,用于下载服务器apk
ionic cordova plugin add cordova-plugin-file-transfer npm install --save @ionic-native/file-transfer
4、安装opener2,用于打开指定目录的apk进行安装
ionic cordova plugin add cordova-plugin-file-opener2 npm install --save @ionic-native/file-opener
准备工作就绪后,开始写实现代码如下:
1、在app.module.ts中引入几个插件
//app升级相关插件 import { AppVersion } from '@ionic-native/app-version'; import { FileOpener } from '@ionic-native/file-opener'; import { FileTransfer, FileTransferObject } from '@ionic-native/file-transfer'; import { File } from '@ionic-native/file'; //app升级相关插件
2、创建一个ionic g provider appUpgrade 服务
完整代码如下:
import { PlatformServiceProvider } from './../platform-service/platform-service'; import { Injectable } from '@angular/core'; import {Platform,AlertController } from 'ionic-angular'; import { Http } from '@angular/http'; import {File} from '@ionic-native/file'; import {FileTransfer,FileTransferObject} from '@ionic-native/file-transfer'; import {FileOpener} from '@ionic-native/file-opener'; import {AppVersion} from '@ionic-native/app-version'; import 'rxjs/add/operator/map'; /* app在线升级自动安装 */ @Injectable() export class AppUpgradeProvider { constructor(public http: Http,private platform: Platform, private alertCtrl: AlertController, private transfer: FileTransfer, private appVersion: AppVersion, private file: File,private fileOpener:FileOpener, private platformService:PlatformServiceProvider) { } /** * 检查app是否需要升级 */ detectionUpgrade(u) { //这里连接后台获取app最新版本号,然后与当前app版本号(this.getVersionNumber())对比 //版本号不一样就需要申请,不需要升级就return this.alertCtrl.create({ title: '升级', subTitle: '发现新版本,是否立即升级?', buttons: [{text: '取消'}, { text: '确定', handler: () => { this.downloadApp(u); } } ] }).present(); } /** * 下载安装app */ downloadApp(u) { if (this.platformService.isAndroid()) { let alert = this.alertCtrl.create({ title: '下载进度:0%', enableBackdropDismiss: false, buttons: ['后台下载'] }); alert.present(); const fileTransfer: FileTransferObject = this.transfer.create(); const apk = this.file.externalRootDirectory + 'jmtec/lingguo.apk'; //apk保存的目录externalRootDirectory // const apk="file:///storage/DownloadZhao/zhaozhao.apk"; // console.log(apk);APK_DOWNLOAD fileTransfer.download(u, apk).then(() => { // window['install'].install(apk.replace('file://', '')); // entry.nativeURL 是上面那个插件文件下载后的保存路径 this.fileOpener.open(apk, 'application/vnd.android.package-archive') .then(() => { console.log('打开成功'); }) .catch(() => { console.log('打开失败'); }); }); fileTransfer.onProgress((event: ProgressEvent) => { let num = Math.floor(event.loaded / event.total * 100); if (num === 100) { alert.dismiss(); } else { let title = document.getElementsByClassName('alert-title')[0]; title && (title.innerHTML = '下载进度:' + num + '%'); } }); } if (this.platformService.isIos()) { // this.openUrlByBrowser(APP_DOWNLOAD); } } /** * 通过浏览器打开url */ openUrlByBrowser(url:string):void { // this.inAppBrowser.create(url, '_system'); } /** * 获得app版本号,如0.01 * @description 对应/config.xml中version的值 * @returns {Promise<string>} */ getVersionNumber(): Promise<string> { return new Promise((resolve) => { this.appVersion.getVersionNumber().then((value: string) => { resolve(value); }).catch(err => { console.log('getVersionNumber:' + err); }); }); } }