基于 ionic 的 apk 自动升级

本文介绍了如何在基于Ionic的应用中实现自动升级功能。通过使用cordova-plugin-app-version判断版本,结合cordova-plugin-file、cordova-plugin-file-transfer和cordova-plugin-file-opener2进行文件操作和升级下载。在服务端设置版本号并与客户端比较,用户可以选择自动或手动升级。在实施过程中遇到了$cordovaFileOpener2引用错误,最终通过更换插件和调整config.xml中文件保存路径解决。

一、所需插件

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.git

config.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" />

将文件保存在该空间的根路径下。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值