您的位置:首页 > 移动开发

Ionic3 Android 检测并下载安装app

2017-09-22 17:36 316 查看
在应用不被发布到应用超市的情况下,而又想实现提醒用户升级APP,除了客户端要获取版本外,还需要服务器有API可以获取到当前维护进来的最新的APP版本号,以及最新的下载地址,废话不多讲了,上代码

首先准备工作,安装需要的插件

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中引入几个插件



添加到providers中



2、在NativeService.ts中实现提示更新、下载apk、打开apk文件进行安装,同样引入几个插件





3、检查app提示是否升级
       
detectionUpgrade(apkUrl, allowChoose) {
if (allowChoose) {
this.alertCtrl.create({
title: '升级提示',
subTitle: '发现新版本,是否立即升级?',
buttons: [{
text: '取消'
}, {
text: '确定',
handler: () => {
this.downloadApp(apkUrl);
}
}]
}).present();
} else {
this.downloadApp(apkUrl);
}
}

4、下载安装app

downloadApp(apkUrl) {
let alert = this.alertCtrl.create({
title: '下载进度:0%',
enableBackdropDismiss: false,
buttons: ['后台下载']
});
alert.present();

const fileTransfer: FileTransferObject = this.transfer.create();
const apk = this.file.externalRootDirectory + 'app.apk'; //apk保存的目录
fileTransfer.download(apkUrl, apk).then(() => {
this.fileOpener.open(apk, 'application/vnd.android.package-archive').then(() =>{
console.log('File is opened')
}).catch(e => {
console.log('Error openening file', e)
});
});
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 + '%');
}
});
}


5、检测是否有新版本,需要服务器提供API检测最新版本

import { Component } from '@angular/core';
import { Platform } from 'ionic-angular';
import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen';
import { Storage } from '@ionic/storage';
import { NativeService } from './../providers/NativeService';
import { AppUpdateService } from '../providers/AppUpdateService';
import { AppVersion } from '@ionic-native/app-version';

@Component({
templateUrl: 'app.html'
})
export class MyApp {
rootPage: any = 'page-tabs';
constructor(platform: Platform, statusBar: StatusBar, splashScreen: SplashScreen, private s: Storage,
private ns: NativeService, private aus: AppUpdateService, private appVersion: AppVersion) {
platform.ready().then(() => {
statusBar.styleDefault();
splashScreen.hide();
this.appVersion.getVersionNumber().then((version: string) => {
this.aus.compari
4000
Version(version).subscribe(data => {
if (data.httpCode == 200) {
if (data.data.latestVersion != null && data.data.latestVersion !== version) {
this.ns.detectionUpgrade(data.data.androidDownload, true); //提示升级
}
}
});
}).catch(err => {
console.log('getVersionNumber:' + err);
});
});
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ionic app升级