码迷,mamicode.com
首页 > 移动开发 > 详细

cordova APP 检查更新

时间:2018-10-26 13:07:24      阅读:232      评论:0      收藏:0      [点我收藏+]

标签:amp   程序   ota   floor   cti   dir   ice   ide   RoCE   

原文:cordova APP 检查更新

//升级程序
    .factory(‘UpdateService‘, function ($rootScope, $cordovaAppVersion, $cordovaFileTransfer, $timeout, $ionicLoading, $cordovaFileOpener2, $http, $ionicPopup, xc) {

        //检查更新
        var checkUpdate = function () {
            $cordovaAppVersion.getVersionNumber().then(function (v) {
                var url = xc.server.domain + ‘update/checkNew/‘ + xc.app.id + ‘/android/‘ + v;
                $http.get(url)
                    .error(function () {
                        console.log(‘从服务器获取数据失败!‘);
                    })
                    .then(function (response) {
                            var data = response.data;
                            var compatible_binary = data.compatible_binary;
                            var update_available = data.update_available;
                            if (compatible_binary && update_available) {
                                var url = xc.server.domain + data.update.url;
                                showUpdateConfirm(url, data.info || ‘‘);
                            }
                        }
                    );
            });
        };

        /**
         *
         * @param url
         * @param info
         */
        var showUpdateConfirm = function (url, info) {

            var confirmPopup = $ionicPopup.confirm({
                title: ‘版本升级‘,
                template: info.toString().replace(/\n/g, ‘<br />‘),
                cancelText: ‘取消‘,
                okText: ‘升级‘
            });

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

                        //console.log(JSON.stringify(result));

                        $cordovaFileOpener2.open(targetPath, ‘application/vnd.android.package-archive‘
                        ).then(function () {

                        }, function (err) {

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

                } else {
                    // 取消更新
                }
            });
        };

        return {
            checkUpdate: checkUpdate
        }
    })

  

cordova APP 检查更新

标签:amp   程序   ota   floor   cti   dir   ice   ide   RoCE   

原文地址:https://www.cnblogs.com/lonelyxmas/p/9855514.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!