dcloud --app版本更新

来源:互联网 发布:unity3d 协程重复调用 编辑:程序博客网 时间:2024/06/05 17:07

dcloud --app版本更新

1.app开发,最主要的一个功能就是版本更新,其实原理很简单,下载最新版本,然后自动打开app,重新安装!

2.提供的实例代码中,存在 update.js这个文件,也许它可以做到更新,不过,本人没用她提供的,自己写了一套,后台代码就不上了,这个其实就是文件下载的功能,意义不大,以下是部分代码
 OutputStream os = null;
        BufferedOutputStream bos = null;
        FileInputStream fis = null;
        BufferedInputStream bis = null;
        try {
            String fileName = String.format("%s%s%s", apkInfo.getTitle(), ".", apkInfo.getExt());
            response.setHeader("conent-type", "application/vnd.android.package-archive");
            response.setContentType("application/vnd.android.package-archive");
            response.setHeader("Content-Disposition", "attachment; filename=" + new String(fileName.getBytes("UTF-8"), "ISO-8859-1"));


            File apkFile = new File(apkInfo.getFileUrl());
            if (!apkFile.exists()) {
                return;
            }
            long fileLength = apkFile.length();
            response.setHeader("Content-Length", String.valueOf(fileLength));


            os = response.getOutputStream();
            bos = new BufferedOutputStream(os);
            fis = new FileInputStream(apkFile);
            bis = new BufferedInputStream(fis);


            byte[] buffer = new byte[1 * 1024 * 10];
            int len = 0;
            while ((len = bis.read(buffer)) != -1) {
                bos.write(buffer, 0, len); // 写入
                bos.flush();
            }
            if (os != null) {
                os.close();
            }
            if (bos != null) {
                bos.close();
            }
            if (fis != null) {
                fis.close();
            }
            if (bis != null) {
                bis.close();
            }
,关键是前台怎么操作。

首先,我们需要校验一下版本,是否是最新版本,代码如下:
var wgtVer=null;
function plusReady(){
// 获取本地应用资源版本号
    plus.runtime.getProperty(plus.runtime.appid,function(inf){
        wgtVer=inf.version;
        //赋值
document.getElementById("nversion").innerHTML="V"+wgtVer;
    });
//得到系统内最新版本信息
var verserver=$app_api_url.findVersion();  //后台代码地址(校验版本号的)
mui.getJSON(verserver, {
"appid": plus.runtime.appid     //这个自己去看 mainfest.json
}, function(data) {
if (data.succeed) {
if(wgtVer!=data.data){
var inwebview = plus.webview.getWebviewById('index');
inwebview.evalJS("showredpoint(true)");
document.getElementById("nversion").innerHTML="V"+wgtVer+"<sup>●</sup>"; 
}
}else{

}
});
}
<sup>●</sup>,这个,我觉得会html的人,应该都要知道,不做解释,inwebview.evalJS,这个其实就是js方法,我这里实现的是,有新版的时候,打一个红色的标记,
//坐标显示红点
function showredpoint(ob){
if(ob==true){
document.getElementById('point').classList.remove("mui-hidden");
}else{
document.getElementById('point').classList.add("mui-hidden");
}
}

以下代码是下载更新的代码
var server = $app_api_url.apkCheck(); //获取升级描述文件服务器地址
var dserver=$app_api_url.apkDownload();//apk 下载


function update() {
mui.getJSON(server, {
"appid": plus.runtime.appid,
"version": wgtVer,
"imei": plus.device.imei
}, function(data) {
if (data.succeed) {
if(data.data==false){
plus.nativeUI.confirm("消息", function(event) {
if (0 == event.index) {
downWgt(plus.runtime.appid);
}
}, data.title, ["立即更新", "取  消"]);
}else{
plus.nativeUI.alert("最新版本");
}
}else{
plus.nativeUI.alert("版本校验失败");
}
});
}

function downWgt(ob){
var wgtUrl=dserver+"?appid="+ob;
    plus.nativeUI.showWaiting("下载更新包...");
    plus.downloader.createDownload( wgtUrl, {method: "GET",filename:"_doc/update/",retry: 0}, function(d,status){
        if ( status == 200 ) { 
            console.log("下载wgt成功:"+d.filename);
            plus.runtime.openFile(d.filename);//打开安装包
        } else {
            console.log("下载wgt失败!");
            plus.nativeUI.alert("下载wgt失败!");
        }
        plus.nativeUI.closeWaiting();
    }).start();
}

plus.downloader.createDownload( ),这个函数,大家自己搜索一下,我只简单的解释一下,wgtUrl,下载的路径,get方式访问,filename 表示文件下载到本手机的路径,plus.runtime.openFile(),该方法表示下载完成后自动打开app,实现安装。

如果大家感兴趣,或者想一起探讨学习的,请加一下这个QQ群, 488505459,谢谢!

原创粉丝点击