一、所需插件
cordova plugin add cordova-plugin-app-version cordova-plugin-file cordova-plugin-file-transfer cordova-plugin-file-opener2 cordova-plugin-network-information其中, cordova-plugin-app-version 用于版本判断。P.S. 版本号写在config.xml的widget的version。
cordova-plugin-file 用于实现在各种设备下对文件、和文件夹进行访问,编辑等各种操作。 cordova-plugin-file-transfer 用于上传和下载文件。 cordova-plugin-file-opener2 用于打开设备下的文件。 cordova-plugin-network-information 用于判断网络连接状态。
二、逻辑流程 客户端版本和服务器返回的版本号作比较,不同则判断当前网络连接状态(wifi和其他),更新是先从服务器把apk包下载下来,最后打开这个apk,安装覆盖原来版本。
正常来说按照上面没有错。但,张工让把版本号自己规定到service里。于是变成,客户端版本和服务器返回的版本号作比较,提示升级。用户选择自动升级或者手动升级(就是退出 = =!)。
三、实现 1、Service下:
.factory('ProdInfos', function() { return { ver: function() { return "1.0.0.0"; }, publishat: function() { return "2017-02-18"; } } })2、Controller下:function下引用上述插件。
// 判断版本自动升级 if (resp.ver != ProdInfos.ver()) { var confirmPopup = $ionicPopup.confirm({ title: '版本升级', template: '当前版本不是最新版本,需升级。', cancelText: '手动升级', okText: '确认', }); confirmPopup.then(function(res) { if(res) { UpdateForAndroid(); } else { $ionicLoading.show({ template: '请尝试手动升级。', noBackdrop: true, duration:1500}); $ionicLoading.hide(); ionic.Platform.exitApp(); } }); } // apk自动升级函数 var UpdateForAndroid = function(){ var url = encodeURI('http://XXX.XX.XX.XX/android-debug.apk'); // 下载地址 var targetPath = "/sdcard/Download/unattended.apk"; var trustHosts = true; var options = {}; $cordovaFileTransfer.download(url, targetPath, options, trustHosts).then(function (result) { window.plugins.webintent.startActivity({ action: window.plugins.webintent.ACTION_VIEW, url: result.toURL(), type: 'application/vnd.android.package-archive' } ); // $ionicLoading.hide(); // $cordovaFileOpener2.open(result.toURL(), 'application/vnd.android.package-archive' // ).then(function () { // // 成功 // }, function (err) { // alert(JSON.stringify(err)); // }); }, function (err) { $ionicLoading.show({template: "下载失败", duration: 1500}); }, function (progress) { var downloadProgress = Math.floor((progress.loaded / progress.total) * 100); if (downloadProgress >= 99) $ionicLoading.hide(); else $ionicLoading.show({template: "已经下载:" + downloadProgress + "% ,<br/>下载过程中请勿退出。" }); }); }四、有错误 这里的 $cordovaFileOpener2 引用不成功。报错: 所以就换了另一个插件:
cordova plugin add https://github.com/Initsogar/cordova-webintent.gitconfig.xml里引入
<plugin name="WebIntent" value="com.borismus.webintent.WebIntent" />sample code:
window.plugins.webintent.startActivity({ action: window.plugins.webintent.ACTION_VIEW, url: theFile.toURL(), type: 'application/vnd.android.package-archive' }, function() {}, function() { alert('Failed to open URL via Android Intent.'); console.log("Failed to open URL via Android Intent. URL: " + theFile.fullPath) } );P.S. https://github.com/Initsogar/cordova-webintent https://github.com/zxj963577494/ionic-AutoUpdateApp http://ngcordova.com/docs/plugins
P.P.S. 错误原因当时不明,但现在整理好像知道为什么了。应该是和 cordova-plugin-file 这个插件有关。 它规定文件的保存位置。如果config.xml中不设置:
<preference name="AndroidPersistentFileLocation" value="Internal" />文件保存在:是保存在 /data/data/packageId 下面(rmclient539542正是我的项目id)。 所以我当时应该设置:
<preference name="AndroidPersistentFileLocation" value="Compatibility" />将文件保存在该空间的根路径下。