看标题就知道今天的坑是夺大的坑。
贴上完整代码吧!
// 检查app是否需要升级
detectionUpgrade() {
//这里连接后台获取app最新版本号,然后与当前app版本号(this.getVersionNumber())对比
this.config.getRemoteVersion((data)=>{
this.versionNum = data.versionName;
console.log("服务端 version num : "+this.versionNum);
});
this.getVersionNumber((data)=>{
this.currentVersionStr = data.substr(data.lastIndexOf(".")+1);
console.log("本地 version num : "+this.currentVersionStr);
this.currentVersionNum = Number(this.currentVersionStr);
});
if(this.versionNum>this.currentVersionNum){
//版本号不一样就需要申请,不需要升级就return
this.alertCtrl.create({
title: '升级',
subTitle: '发现新版本,是否立即升级?',
buttons: [{ text: '取消' },
{
text: '确定',
handler: () => {
this.downloadApp();
}
}
]
}).present();
}
}
// 下载安装app
downloadApp() {
if (this.isAndroid()) {
let alert = this.alertCtrl.create({
title: '下载进度:0%',
enableBackdropDismiss: false,
buttons: ['取消']
});
alert.present();
const fileTransfer: FileTransferObject = this.transfer.create();
const apk = this.file.externalApplicationStorageDirectory + 'android.apk'; //apk保存的目录
fileTransfer.download(this.config.host+this.config.updateFile, 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 + '%');
}
});
}
if (this.isIos()) {
this.openUrlByBrowser("这里边填写下载iOS地址");
}
}
我踩的坑是,下载安装包的时候,本地下载路径找不到。
官方文档里,file可选的路径有:
/**
* Read-only directory where the application is installed.
*/
applicationDirectory: string;
/**
* Read-only directory where the application is installed.
*/
applicationStorageDirectory: string;
/**
* Where to put app-specific data files.
*/
dataDirectory: string;
/**
* Cached files that should survive app restarts.
* Apps should not rely on the OS to delete files in here.
*/
cacheDirectory: string;
/**
* Android: the application space on external storage.
*/
externalApplicationStorageDirectory: string;
/**
* Android: Where to put app-specific data files on external storage.
*/
externalDataDirectory: string;
/**
* Android: the application cache on external storage.
*/
externalCacheDirectory: string;
/**
* Android: the external storage (SD card) root.
*/
externalRootDirectory: string;
/**
* iOS: Temp directory that the OS can clear at will.
*/
tempDirectory: string;
/**
* iOS: Holds app-specific files that should be synced (e.g. to iCloud).
*/
syncedDataDirectory: string;
/**
* iOS: Files private to the app, but that are meaningful to other applications (e.g. Office files)
*/
documentsDirectory: string;
/**
* BlackBerry10: Files globally available to all apps
*/
sharedDirectory: string;
其中external开头的是外部存储空间的路径,我们一般安装app是在这里,也就是/storage/emulated/0/Android/data/包名,我们更新apk最好也把它放在这里。
然后再配置AndroidManifast.xml:
这里,有个opener_paths,检查一下
路径是否有问题,实在不确定可以打印出来看看。
FileOpener2.java文件里,path对应的就是我们的下载路径。
for循环可以把该赋的权限赋了。
另外关于我注释的那部分,因为我这个安装包的客户基本上不会有android6以下的使用者了,所以就直接把版本检测去掉了,如有必要,大家还是加上的好,不要学我……