http://www.javashuo.com/article/p-uhiexsdu-cs.htmlhtml
放到cordova.file.DataDirectory下載異常? 只好cordova.file.externalDataDirectoryandroid
// 檢查更新 $scope.appUpdateCheck = function(init) { var init = init || 0; // appUpdateService.showUpdateConfirm({}); // return; VersionService.get({ platform: ionic.Platform.platform() }, function(resp) { // alert(JSON.stringify(resp)) if (resp.code == '000') { // alert(resp.data.version) // alert($cordovaAppVersion.getVersionNumber()) // alert($cordovaAppVersion.getVersionCode()) cordova.getAppVersion.getVersionNumber(function(version) { // alert(version) if (version == resp.data.version) { if (!init) { messageService.show('當前已經是最新版本'); } return; } $scope.hasNewVersion = 1; $scope.device_items[1].name = $sce.trustAsHtml('升級檢查<font style="color:#FD1F38"> - New!</font>'); if (!init) { appUpdateService.showUpdateConfirm(resp.data); } }); } else { messageService.show(resp.msg); } }); }
; (function(angular) { angular.module('mobile') .factory('appUpdateService', ['$cordovaFileTransfer', '$cordovaFileOpener2', '$ionicPopup', '$ionicLoading', '$timeout', 'messageService', function($cordovaFileTransfer, $cordovaFileOpener2, $ionicPopup, $ionicLoading, $timeout, messageService) { // 顯示是否更新對話框 function showUpdateConfirm(versionInfo) { var versionInfo = versionInfo; var downloadUrl = versionInfo.url, updateLog = versionInfo.upgradeinfo, targetPath = 'file:///storage/sdcard0/Download/XiaoShuTong.apk' //APP下載存放的路徑,可使用cordova file插件進行相關配置 // TODO 測試數據 // downloadUrl = 'http://imtt.dd.qq.com/16891/006473EB7690D8B89DEBD9613BF9E40E.apk?fsname=com.tencent.mm_6.6.0_1200.apk' // updateLog = '1. 更新模塊升級<br>2. 修復1個bug<br>' targetPath = cordova.file.externalDataDirectory + 'XiaoShuTong.apk' var confirmPopup = $ionicPopup.confirm({ title: '版本升級', template: updateLog, //從服務端獲取更新的內容 cancelText: '取消', okText: '升級' }); confirmPopup.then(function(res) { if (res) { // if (ionic.Platform.isIOS()) { // // 提示進入 APP store // messageService.show('請到App Store升級.'); // } if (ionic.Platform.isAndroid() && downloadUrl != '') { $ionicLoading.show({ template: "已經下載:0%" }); $cordovaFileTransfer.download(downloadUrl, targetPath, null, true).then(function(result) { // 打開下載下來的APP $cordovaFileOpener2.open(targetPath, 'application/vnd.android.package-archive').then(function() { // 成功 }, function(err) { // 錯誤 // alert('安裝文件打開失敗, 請嘗試手動安裝.'); messageService.show('安裝文件打開失敗, 請嘗試手動安裝.'); }); $ionicLoading.hide(); }, function(error) { $ionicLoading.hide(); // alert(JSON.stringify(error)); // messageService.show(JSON.stringify(error)); messageService.show('請設置應用文件操做權限, 而後重試.'); }, function(progress) { var downloadProgress = Math.floor((progress.loaded / progress.total) * 100); if (downloadProgress > 0) { $ionicLoading.show({ template: '已經下載:' + downloadProgress + '%' }); } if (downloadProgress > 99) { $ionicLoading.hide(); } }); } } else { // 取消更新 $ionicLoading.hide(); } }); } // return return { showUpdateConfirm: showUpdateConfirm } } ]); })(angular);