cordova app 升级

来源:互联网 发布:淘宝店怎么经营管理 编辑:程序博客网 时间:2024/06/06 12:54
//升级程序
app.run(function ($rootScope, $cordovaAppVersion, $cordovaFileTransfer, $timeout
    , $ionicLoading, $cordovaFileOpener2,$http, $ionicPopup) {

    //apk 更新下载地址
    $rootScope.apk_url = 'http://192.192.192.192/iot_androidApp.apk';
    $rootScope.apk_download_process = 100;
    $rootScope.fn_download_apk = function (url) {

        var confirmPopup = $ionicPopup.confirm({
            title: '温馨提示:',
            template: '发现新版本,现在升级?',
            cancelText: '取消',
            okText: '升级'
        });

        confirmPopup.then(function (res) {
            if (res) {
                $rootScope.apk_download_process = 0;
                $ionicLoading.show({
                    template: '<ion-spinner icon="bubbles" class="spinner-assertive spinner spinner-bubbles"></ion-spinner>' +
                    '<br>已经下载:{{apk_download_process}}%'
                });
                //APP下载存放的路径,可以使用cordova file插件进行相关配置
                var targetPath = cordova.file.externalDataDirectory + "update.apk";
                var trustHosts = true;
                var options = {};
                $cordovaFileTransfer.download(url, targetPath, options, trustHosts).then(function (result) {
                    //测试下载成功后的信息  - - JSON.stringify(result)
                    //$rootScope.fn_test_demo(targetPath);
                    //$rootScope.fn_test_demo(JSON.stringify(result));

                    //检查一个应用程序是否已经安装   --   好使滴
                    //$cordovaFileOpener2.appIsInstalled('com.chen.cws').then(function(res) {
                    //    if (res.status == 0) {
                    //        // is not installed.
                    //        $rootScope.fn_test_demo('do not installed');
                    //    } else {
                    //        //  is installed.
                    //        $rootScope.fn_test_demo('do installed');
                    //
                    //        //卸载 指定 Activity 的 app   --   好使滴
                    //        //$cordovaFileOpener2.uninstall('com.chen.cws').then(function() {
                    //        //    // Uninstall intent activity started.
                    //        //    }, function(err) {
                    //        //    // An error occurred. Show a message to the user
                    //        //    });
                    //    }
                    //});

                    // 打开下载下来的APP   --  在模拟器上测试是不行的,但是在真正手机上就可以。
                    $cordovaFileOpener2.open(
                        targetPath, 'application/vnd.android.package-archive'
                    ).then(function () {
                        //$rootScope.fn_test_demo('打开下载下来的APP');
                    }, function (err) {

                    });

                    $ionicLoading.hide();
                }, function (err) {

                }, function (progress) {
                    //进度,这里使用文字显示下载百分比
                    $timeout(function () {
                        var downloadProgress = (progress.loaded / progress.total) * 100;
                        $rootScope.apk_download_process = Math.floor(downloadProgress);
                        if (downloadProgress > 99) {
                            $ionicLoading.hide();
                        }
                    })
                });

            } else {
                // 取消更新
            }
        });
    };
    $rootScope.fn_test_demo = function(info) {
        var confirmPopup = $ionicPopup.confirm({
            title: '温馨提示:',
            template: info
        });
        confirmPopup.then(function(res) {
            if(res) {

            } else {
                //console.log('You are not sure');
            }
        });
    };
    $rootScope.fn_updateApk = function(info) {
        //手动自己输入 -- 如果有apk跟新,就输入 -- 也要修改 config.xml里的版本号
        var getAppVersion = '1.0.6';
        //获取版本号 -- 注意:执行此语句,必须等待DOM元素加载完毕 所以加个延迟
        $timeout(function(){
            cordova.getAppVersion.getVersionNumber().then(function(version){
                //$rootScope.fn_test_demo(version);
                if( getAppVersion != version ){
                    $rootScope.fn_download_apk( $rootScope.apk_url );
                }
            });
        },10000);

    };
    $rootScope.fn_updateApk();
}];
原创粉丝点击