Cordova ionic3打包 APP在線自動更新升級踩坑問題

做了三天踩了無數的坑,摸了無數的魚,看了無數的帖子,針對Android版本有很多解決方案,在這裏本人用的技術是ionic3+angular5,其他版本的用我的可能不能解決問題但是可以參考。

本貼子參考了好幾個大神的技術精煉而成

帖子地址: https://www.jianshu.com/p/56c101672372  主要的就是這個

主要使用插件  file  file-transfer  file-opener2

首先 file-opener2 一個坑,如果報錯的話請使用 2.0.19版本,

其他的版本主要爲

// 文件管理 
$ ionic cordova plugin add [email protected]
$ npm install --save @ionic-native/[email protected]
// 文件上傳 
$ ionic cordova plugin add [email protected]
$ npm install --save @ionic-native/[email protected]

$ ionic cordova plugin add [email protected]
$ npm install --save @ionic-native/[email protected]

這個坑我踩了大半天,下載成功之後 老是報錯。object() not function 啥的e.open not function 這樣的錯,打不開APK 

解決之後 還有不能打開的問題==》platforms/app/src/main/AndroidManifest.xml 裏

<uses-sdk android:minSdkVersion="19" android:targetSdkVersion="26" />修改

<uses-sdk android:minSdkVersion="19" android:targetSdkVersion="23" />

然後打開APK成功,這時候遇到了解析包出錯

是因爲

下載下來的.APK文件放在內存裏面 了,就是/data/data/你的包名下面    

然後修改到手機內存中,

使用這個保存地址就可以了

下邊上代碼

 

import {AlertController, App, Platform} from "ionic-angular";
import {HttpProvider} from "../http/http";
import {ApiServiceProvider} from "../api-service/api-service";
import {Injectable} from "@angular/core";
import {File} from '@ionic-native/file';
import {FileTransfer, FileTransferObject} from '@ionic-native/file-transfer';
import {FileOpener} from '@ionic-native/file-opener';

@Injectable()
export class UpdateAppProvider {

    //控制硬件返回按鈕是否觸發,默認false
    backButtonPressed: boolean = false;
    versionNumber: number = 0;//本地版本號
    webVersionNumber: number = 0;//服務器版本號
    updateProgress: number = 0;

    //構造函數 依賴注入
    constructor(public platform: Platform, private file: File, private transfer: FileTransfer, private fileOpener: FileOpener, private alertCtrl: AlertController, public http: HttpProvider, public apiService: ApiServiceProvider, public appCtrl: App) {
    }

    checkVersion() {
        this.http.get(this.apiService.url.getSgpAppVersion, {}).subscribe(res => {
            this.webVersionNumber = res.result.versioncode;
            console.log(this.webVersionNumber)
            if (this.versionNumber !== this.webVersionNumber && this.webVersionNumber != null) {
                this.alertCtrl.create({
                    title: '版本升級:' + res.result.name,
                    subTitle: res.result.description,
                    enableBackdropDismiss: false,
                    buttons: [{
                        text: '確定', handler: () => {
                            this.updateApp(res.result.fileurl);
                        }
                    }
                    ]
                }).present();
            }
        }, error => {
            console.error(`錯誤信息:${JSON.stringify(error)}`);
        });
    }
    /**
     * 下載安裝app
     */
    updateApp(url) {
        var alert = this.alertCtrl.create({
            title: '下載進度:0%',
            enableBackdropDismiss: false
        });
        alert.present();
        console.log("彈出下載框")
        var backgroundProcess = false; // 是否後臺下載
        var apkUrl = 'akpurl' + url;//apk下載地址
        const fileTransfer: FileTransferObject  = this.transfer.create();
        const apk = this.file.externalRootDirectory  + 'download/' + 'xhxy.apk'; // 下載apk保存的目錄
        console.log("下載地址:"+apkUrl)
        console.log("保存目錄:"+apk)
        console.log(this.file.dataDirectory)
        let timer = null; // 由於onProgress事件調用非常頻繁,所以使用setTimeout用於函數節流
        fileTransfer.onProgress((event: ProgressEvent) => {
            const progress = Math.floor(event.loaded / event.total * 100); // 下載進度
            this.updateProgress = progress;
                    if (progress === 100) {
                        alert && alert.dismiss();
                    } else {
                        if (!backgroundProcess) {
                            const title = document.getElementsByClassName('alert-title')[0];
                            title && (title.innerHTML = '下載進度' + progress + '%');
                        }
                    }
        });

        fileTransfer.download(encodeURI(apkUrl), apk).then((entry) => {
                this.fileOpener.open(apk, 'application/vnd.android.package-archive').then(() => console.log('打開apk包成功!'))
                    .catch(e => console.log('打開apk包失敗!', e));
        }, err => {
            this.updateProgress = -1;
            this.alertCtrl.create({
                title: '提示',
                subTitle: '本地升級失敗',
                buttons: [{
                    text: '確定'
                }]
            }).present();
        });

    }

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章