原文:Cordova app 检查更新 ----JS进行调用(二)

1.获取版本号 需要添加 插件

cordova plugin add https://github.com/whiteoctober/cordova-plugin-app-version.git

js 进行调用:
// 获取当前移动设备已经安装的版本
cordova.getAppVersion.getVersionNumber().then(function (version) {
var versionCode = parseInt(version.toString().replace(/\./g,''));
console.log(versionCode);
// 1.获取当前版本号
// 2.获取服务器端的最新版本的数据源
// 3.进行版本比较,如果当前的版本号与服务器的版本号不一致时,下载并安装最新的应用程序安装包
Common.alert("温馨提示", "有新版本可供更新.\n 1.界面美化 \n 2.性能优化\n 3.最新版本:"+versionCode); });

2. 使用ajax进行获取服务中最新的安装包的详细信息

3.比较版本号是否一致,如果不一致,将会提示是否进行“更新”

4.如果选择"更新",对执行安装包的下载

var url = "http://ftp-apk.pconline.com.cn/5014afca744eddb3fc7fe2d25d60aa29/pub/download/201010/HiMarket.apk?crazycache=1"; //可以从服务端获取更新APP的路径
var targetPath = "HiMarket.apk"; //APP下载存放的路径,可以使用cordova file插件进行相关配置
var trustHosts = true;
var options = {};
var ft = new FileTransfer();
ft.download(url, targetPath, successCallback, fileTransfer.downloadFailed, trustHosts, options);

5.将打开下载后的安装包

Android 平台下打开安装包文件的方式与iOS的方式不一样

Android使用本地路径进行打开

iOS需要进行跳转到app store 中进行下载更新

此时需要将FileOpener2.h中定义的方法添加到plugins.FileOpener2.js (iOS项目中的cordova-plugin-file-opener2目录下的www目录中)

cordova.define("cordova-plugin-file-opener2.FileOpener2", function(require, exports, module) {
/*jslint browser: true, devel: true, node: true, sloppy: true, plusplus: true*/
/*global require, cordova */ var exec = require('cordova/exec'); function FileOpener2() {} FileOpener2.prototype.openURL = function (url, callbackContext) {
callbackContext = callbackContext || {};
exec(callbackContext.success || null, callbackContext.error || null, 'FileOpener2', 'openURL', [url]);
}; FileOpener2.prototype.open = function (fileName, contentType, callbackContext) {
callbackContext = callbackContext || {};
exec(callbackContext.success || null, callbackContext.error || null, 'FileOpener2', 'open', [fileName, contentType]);
}; FileOpener2.prototype.uninstall = function (packageId, callbackContext) {
callbackContext = callbackContext || {};
exec(callbackContext.success || null, callbackContext.error || null, 'FileOpener2', 'uninstall', [packageId]);
}; FileOpener2.prototype.appIsInstalled = function (packageId, callbackContext) {
callbackContext = callbackContext || {};
exec(callbackContext.success || null, callbackContext.error || null, 'FileOpener2', 'appIsInstalled', [packageId]);
}; module.exports = new FileOpener2();
});

6.将这两种文件的打开方式进行封装成FileOpen.js

/**
* 使用方法:
* fileOpener.open(url).then(function(){});
* 用到的原生api 有:cordova-plugin-dialogs,cordova-plugin-device,cordova-plugin-FileTransfer
*/ /**FileTransfer*/
var ft;
Ext.define("util.FileOpen", {
alternateClassName: "fileOpener",
singleton: true,
config: {
isComplete: false,
description: '高视医疗信息'
},
constructor: function (config) { this.initConfig(config);
},
// Returns a jQuery or AngularJS deferred object, or pass a success and fail callbacks if you don't want to use jQuery or AngularJS
getPromisedExec: function (command, success, fail) {
var toReturn, deferred, injector, $q;
if (success === undefined) {
if (window.jQuery) {
deferred = jQuery.Deferred();
success = deferred.resolve;
fail = deferred.reject;
toReturn = deferred;
} else if (window.angular) {
injector = angular.injector(["ng"]);
$q = injector.get("$q");
deferred = $q.defer();
success = deferred.resolve;
fail = deferred.reject;
toReturn = deferred.promise;
} else if (window.Promise) {
toReturn = new Promise(function (c, e) {
success = c;
fail = e;
});
} else if (window.WinJS && window.WinJS.Promise) {
toReturn = new WinJS.Promise(function (c, e) {
success = c;
fail = e;
});
} else {
return console.error('AppVersion either needs a success callback, or jQuery/AngularJS/Promise/WinJS.Promise defined for using promises');
}
} if (device.platform == "Android") { cordova.plugins.fileOpener2.open(command, 'application/vnd.android.package-archive', success, fail);
}
else if (device.platform == "iOS") {
// 加载其他应用,应用在升级的时候,iOS使用openURL打开、Android使用open打开 'tel://10086'
cordova.plugins.fileOpener2.openURL(command, {
success: success,
error: fail
});
} return toReturn;
},
open: function (filePath, success, fail) {
return this.getPromisedExec(filePath, success, fail);
}
});

使用方法:

1.比较版本号

cordova.getAppVersion.getVersionNumber().then(function (version) {
var versionCode = parseInt(version.toString().replace(/\./g,''));
// 此处写比较版本号逻辑,需要获取服务器上应用程序的最新版本号 });

2.下载安装包,并执行安装

       Progress.start("正在下载,请稍后...");
// 下载安装包
fileTransfer.download(function(entry){ Progress.close(); // 打开已经下载的安装包
fileOpener.open(entry.fullPath).then(function(message){
console.log(message);
},function(message){
console.log(message);
}); console.log('download complete: ' + entry.fullPath);
console.log('download name: ' + entry.name); });

   

Cordova app 检查更新 ----JS进行调用(二)的更多相关文章

  1. Cordova app 检查更新 ----创建项目、添加插件、修改插件(一)

    原文:Cordova app 检查更新 ----创建项目.添加插件.修改插件(一) 使用Cordova 进行跨平台应用程序的开发 1.创建Cordova项目 $ cordova create hell ...

  2. cordova APP 检查更新

    原文:cordova APP 检查更新 //升级程序 .factory('UpdateService', function ($rootScope, $cordovaAppVersion, $cord ...

  3. Cordova CLI源码分析(二)——package.json

    每个包需要在其顶层目录下包含一个package.json文件,该文件不仅是包的说明,也影响npm安装包时的配置选项 更多参数详见参考文档https://npmjs.org/doc/json.html ...

  4. 小程序公用js提取到app.js中调用的实例

    index.wxml: <view "> <text>{{page}}</text> </view> <view "> ...

  5. [Phonegap+Sencha Touch] 移动开发76 让cordova app訪问远端站点也能调用cordova插件功能

    原文链接:http://blog.csdn.net/lovelyelfpop/article/details/50735395 我相信.应该会有一些cordova开发人员想过实现以下这种app: 使用 ...

  6. Win10 UWP开发系列:使用VS2015 Update2+ionic开发第一个Cordova App

    安装VS2015 Update2的过程是非常曲折的.还好经过不懈的努力,终于折腾成功了. 如果开发Cordova项目的话,推荐大家用一下ionic这个框架,效果还不错.对于Cordova.PhoneG ...

  7. WebView使用详解(一)——Native与JS相互调用(附JadX反编译)

    念念不忘,必有回响,永远坚持你所坚持的! 一直在用WebView,还没有系统的总结过它的用法,下面就系统的总结下,分享给大家 一.基本用法 1.加载在线URL void loadUrl(String ...

  8. iOS(UIWebView 和WKWebView)OC与JS交互 之二

    在iOS应用的开发过程中,我们经常会使用到WebView,当我们对WebView进行操作的时候,有时会需要进行源生的操作.那么我记下来就与大家分享一下OC与JS交互. 首先先说第一种方法,并没有牵扯O ...

  9. Android-webview和js互相调用

    Android-webview和js互相调用 Android 和 H5 都是移动开发应用的非常广泛.市面上很多App都是使用Android开发的,但使用Android来开发一些比较复杂附属类,提示性的 ...

随机推荐

  1. Android Mvvm模式的理解

    1. Mvvm是什么,Mvvm是怎么来的?Mvvm模式广泛应用在WPF项目开发中,使用此模式可以把UI和业务逻辑分离开,使UI设计人员和业务逻辑人员能够分工明确. Mvvm模式是根据MVP模式来的,可 ...

  2. Instant Client 配置

    Instant Client Download 选择  Instant Client for Microsoft Windows (32-bit)  由于PL/SQL Developer 不支持64b ...

  3. 2、HZK和FreeType的使用

    HZK16汉字库的使用 定义如下: unsigned char str[]="我" 在运行时str被初始化为2个字节长度,内容为“我”的GBK码,为:0xCE(区码),0xD2(位 ...

  4. 手把手教你----MyEclipse中 配置 Tomcat

    电脑上配置Tomcatserver 安装Tomcat并配置环境变量 測试是否配置成功 MyEclipse中配置Tomcat 想要开发Java Web的程序.首先在MyEclipse中必须配置Tomca ...

  5. 页面中如何引用外部的HTML(四种方法)

    页面中如何引用外部的HTML(四种方法) 一.总结 一句话总结:a.iframe标签        b.ajax引入代码片段        c.link import的方法导入        d.re ...

  6. widow下svn上传项目时的文件可执行权限问题

    还是项目上发现的问题,要上传Android的源码项目.这里客户端是windows的机器, 测试后发现俩个问题. 1. 文件后缀是.so的文件默认上传不了.    2. 文件后缀是.sh的文件,上传后, ...

  7. 35、在编译Linux内核中增加程序需要完成以下3项工作

    在编译Linux内核中增加程序需要完成以下3项工作: 将编写的源代码拷入Linux内核源代码的相应目录. 在目录的Kconfig文件中增加关于新源代码对应项目的编译配置选项 在目录的Makefile文 ...

  8. [Angular Unit Testing] Debug unit testing -- component rendering

    If sometime you want to log out the comonent html to see whether the html render correctly, you can ...

  9. Swift入坑系列—集合类型

    数组(Arrays) 字典(Dictionaries) 数组(Arrays) 在OC里面,NSArray和NSMutableArray这两个类可以存储任意类型的对象,并且不提供所返回对象的任何特别信息 ...

  10. 嵌入式/X86下linux系统死机及内存优化

    一.  CPU 过高或死锁导致系统卡死 1. CPU占用过高 (1)开线程太多导致CPU占用过高,系统卡死 解决:优化应用层业务逻辑,有些业务不必开线程就不开 (2)频繁清缓存导致读spi-flash ...