vaenow / cordova-plugin-app-update

App updater for Cordova/PhoneGap
MIT License
299 stars 146 forks source link

如何知道检查完成 - Resolved #12

Closed YoungBot closed 7 years ago

YoungBot commented 7 years ago

VAENOW 你好,

我现在的遇到的麻烦是,他还在检查更新但是旧的程序已经开始运行了。有没有什么function类似onCheckComplete,就是当你检查完了以后才能运行下一步?

我测试了onFail和onSuccess这两个callback,测试结果好像是只有在没法连接服务器的时候才会call onFail, 不管有没有更新都会显示onSuccess。有没有什么方法能知道到底需不需要更新呢?

谢谢!

vaenow commented 7 years ago
  1. checkAppUpdate 的返回状态码可以在这个文件查询 Constants.java
    /* 下载中 */
    int DOWNLOAD = 1;
    /* 下载结束 */
    int DOWNLOAD_FINISH = 2;
    /* 点击开始下载按钮*/
    int DOWNLOAD_CLICK_START = 3;

    /**
     * 对比版本号
     */
    int VERSION_COMPARE_START = 200; //private 开始对比版本号; start to compare version
    int VERSION_NEED_UPDATE = 201; //检查到需要更新; need update
    int VERSION_UP_TO_UPDATE = 202; //软件是不需要更新;version up to date
    int VERSION_UPDATING = 203; //软件正在更新;version is updating

    /**
     * 版本解析错误
     */
    int VERSION_RESOLVE_FAIL = 301; //版本文件解析错误 version-xml file resolve fail
    int VERSION_COMPARE_FAIL = 302; //版本文件对比错误 version-xml file compare fail

    /**
     * 网络错误
     */
    int REMOTE_FILE_NOT_FOUND = 404;
    int NETWORK_ERROR = 405;

    /**
     * 没有相应的方法
     */
    int NO_SUCH_METHOD = 501;

    /**
     * 未知错误
     */
    int UNKNOWN_ERROR = 901;
  1. 如何查看返回状态码?

https://github.com/vaenow/cordova-plugin-app-update-demo/blob/master/www/js/index.js#L44

    function onSuccess() {
        console.log('success', JSON.stringify(arguments), arguments);
        //  success {"0":{"code":201,"msg":"success, need date."}}
    }
YoungBot commented 7 years ago

谢谢你的回复!我已经得到了返回状况码。 我现在又有两个问题: 1.我在OnSuccess function 里面用 arguments[0].code 来的到返回状况吗。但是如果arguments[0]不存在,那么程序就崩溃了。能否保证这个arguments[0]必定存在?或者会不会存在arguments[1]或者arguments[2]? 2.我使用了另一个plugin来做target file update,如果有新的目标文件更新,他会在下载结束后 refresh 整个程序。但是这样做的话window.AppUpdate.checkAppUpdate(onSuccess, onFail, updateUrl); 里面的callback不会运行。我得到的log是 Attempted to send a second callback for ID: AppUpdate1451543198 Result was: {"code":202,"msg":"success, up to date."}, 成功和失败两个callback里的代码都不会运行。 我google了一下这个问题,有人说添加代码 PluginResult result = new PluginResult(PluginResult.Status.OK, "YOUR_MESSAGE"); // PluginResult result = new PluginResult(PluginResult.Status.ERROR, "YOUR_ERROR_MESSAGE"); result.setKeepCallback(true); callbackContext.sendPluginResult(result); 用这个方法使callback能被多次调用,但是我不知道在哪里添加这个。我感觉可能这个是要更改plugin的代码。 有什么好的解决方法吗?

谢谢!

vaenow commented 7 years ago

这种情况你可以试试

     console.log(arguments[0] && arguments[0].code);

你可以尝试把refresh移到onSuccess和onFail里面,保证检查更新的流程结束后,再执行refresh

    window.AppUpdate.checkAppUpdate(onSuccess, onFail, updateUrl);

    function onSuccess(){ refresh() }
    function onFail(){ refresh() }   

    function refresh(){
         //refresh app
    }
YoungBot commented 7 years ago

谢谢你 VAENOW, 我现在的代码是这样的:

function checkAppVersionUpdate() {
    var updateUrl = "http://192.168.0.11/version.xml";
    var versionCode = AppVersion.build;
    console.log('version code: ' + versionCode);
    window.AppUpdate.checkAppUpdate(onSuccess, onFail, updateUrl); 
    var me = this;
    function onFail() {
    console.log('check file update');
    checkAppFileUpdate();
    }
    function onSuccess() {          
        if (arguments[0].code == 202) 
        {
            console.log('no need update');
            checkAppFileUpdate();
        }
        else
        {
            console.log('apk needs update');
        }
    }
}

function checkAppFileUpdate(){
     var fs = new CordovaPromiseFS({
        Promise: Promise
        }); 
        // Initialize a CordovaAppLoader
        var loader = new CordovaAppLoader({
        fs: fs,
        serverRoot: 'http://192.168.0.11/manifest/www/',
        localRoot: 'app',
        cacheBuster: true, // make sure we're not downloading cached files.
        checkTimeout: 10000 // timeout for the "check" function - when you loose internet connection
        });
    var needToUpdate = loader.check().then(function(){
        console.log('check finished');
        return loader.download();
        //download to this location cdvfile://localhost/persistent/app
    }).then(function(){
        console.log('download finished');
        return loader.update().then(parseManifestFile());
    },
    function(err){
      console.error('UPDATE ERROR:'+ err);
      parseManifestFile();
    });
  }

我是在checkAPKUpdate 这个函数里面当OnFail或者OnSuccess无需更新时候调用的checkAppFileUpdate函数。 在checkFileUpdate里面的 loader.update()函数是做refresh。但是这样的结果就是我之前发的那段log。然后就没法重新继续运行程序。因为没法调用OnSuccess或者OnFail。

donkeyban commented 7 years ago

您好 請問window.AppUpdate.checkAppUpdate(onSuccess, onFail, updateUrl); 其中updateUrl一定要是一個網路上的xml文件嗎? 可以直接傳入一個xml格式的字串嗎? 因為目前我是直接傳入字串,但是都出現405錯誤 謝謝您

vaenow commented 7 years ago

@YoungBot 具体的业务逻辑还需要你去调试。 如果发现有需要改进的地方,请继续留言。 👍

@donkeyban 目前updateURL只支持文件地址 😃

YoungBot commented 7 years ago

你好VAENOW, 谢谢你之前的答复。我现在用了另一个方法规避了callback的问题。我现在想请教你,如果我想把移至后台下载这个选项按钮去除,我该怎么做?我可以简单的在plugin的java文件里面把代码注释掉还是我需要用别的方法? 谢谢!

vaenow commented 7 years ago

@YoungBot 可以注释掉相关的代码就好了

rohananarse commented 5 years ago

i guys im still stuck here using ionic 3 how do i use this check condition of 202 (right now app keeps on updating itself even if i update)