Appcan使用uexWidget.installApp制作更新程序
  • 分类:经验分享
  • 发表:2017-04-23
  • 围观(2,328)
  • 评论(0)

思路步骤:向服务器请求当前版本,如果服务器版本高于当前版本则返回新版本号,更新日志,新版下载地址,然后APP端下载新版APK并进行安装。

代码如下:

appcan.widgetOne.getCurrentWidgetInfo(function(error, data) {
    var json = parse_result(data);
    set_val('version', json.version)
});

setTimeout(check_update(), 500);

//检查新版本并下载更新
function check_update() {
    ajax({
        data: {
            script_name: 'version',
            act: 'check_update',
            version: get_val('version'),
            os: platform()
        },
        success: cb_check_update,
    })
}

function cb_check_update(data) {
    var json = parse_result(data);
    xie = '\/';
    xie2 = '/'db_url = json.db_url db_url = db_url.replace(xie, xie2);
    set_val('db_url', db_url) var xie = new RegExp("br", "g");
    xie2 = '\n'content = json.content content = content.replace(xie, xie2);
    switch (json.error) {
    case 1:
        if (platform() == 'android') {
            open_confirm({
                title: json.message,
                message: content,
                callback: function(data) {
                    if (data == 1) {
                        update_new()
                    }
                }
            })

        } else if (platform() == 'ios') {
            open_browser(content)
        }
        break
    }
}

function update_new() {
    //安装下载开始
    var db_url = pop_val('db_url');
    var down_path = 'file:///sdcard/huashengbaihuo.apk';
    var downloader = uexDownloaderMgr.create();
    var down_logings = '<div class="updata_apk" id="updata_apk"><div class="uv tx-c uinn-p2">正在下载</div><div class="clear1"></div><div class="ub" id="logding"><span id="logdings"></span></div></div>'$('body').after(down_logings);
    uexDownloaderMgr.download(downloader, db_url, down_path, 1,
    function(fileSize, percent, status) {
        switch (status) {
        case 0:
            $("#logdings").css('width', percent + "%");
            return;
            break;
        case 1:
            $("#updata_apk").addClass('uhide') uexDownloaderMgr.closeDownloader(downloader) setTimeout(function() {
                uexWidgetOne.exit(0)
            },
            100);
            uexWidget.installApp(down_path);

            break;
        case 2:
            alert("文件创建失败") uexDownloaderMgr.closeDownloader(downloader) break;
        }
    });

}

 

结束。

Top