|
楼主 |
发表于 2016-9-7 16:03:49
|
显示全部楼层
本帖最后由 hkdwolf 于 2016-9-7 17:48 编辑
哈哈。。我一直以为斑猪是“liangyongfei”。。
我已经解决了,加了行代码在运行apk的时候关闭APP(原因还是没想明白):
navigator.app.exitApp();
=========分享下代码,可以实现安卓更新时不打开浏览器后台更新:=============
UI2/system/components/justep/versionchecker/versionchecker.js
define(function(require) {
var $ = require('jquery');
var versionInfo;
var Browser = require('$UI/system/lib/base/browser');
var MsgDialog = require("$UI/system/components/justep/messageDialog/messageDialog");
var fileApi = require("$UI/system/components/justep/docCommon/fileApi");
require("cordova!cordova-plugin-app-version");
require("cordova!cordova-plugin-inappbrowser");
require("cordova!cordova-plugin-x-toast");
require("cordova!org.apache.cordova.file");
require("cordova!org.apache.cordova.file-transfer");
require("cordova!org.apache.cordova.file-opener2");
var parentNode = $('body').find('.window').get(0);
var VersionChecker = function() {
this.env = "browser";
if (Browser.isX5App) {
if (Browser.isAndroid) {
this.env = "androidApp";
}
if (Browser.isIOS) {
this.env = "iosApp";
}
}
//this.env = "androidApp";
this.versionMsg = new MsgDialog({
parentNode : parentNode
});
var $domNode = this.versionMsg.$domNode;
$domNode.find('.Yes').text('更新');
$domNode.find('.No').text('忽略');
this.versionMsg.on('onYes', function(event) {
//下载
this.download();
}, this);
this.versionMsg.on('onNo', function(event) {
//忽略
this.ignore();
}, this);
this.resourceInfo = window.__justep.versionInfo.resourceInfo;
this.appInfo = window.__justep.versionInfo.appInfo;
};
VersionChecker.prototype.check = function() {
if (this.resourceInfo) {
this.checkResourceVersion();
}
if (this.appInfo) {
this.checkAppVersion();
}
};
VersionChecker.prototype.checkResourceVersion = function() {
try {
if (navigator.appUtils) {
var versionInfo = window.__justep.versionInfo;
var resourceDownlaodUrl = versionInfo.resourceInfo.downloadURL;
var mode = versionInfo.resourceInfo.mode;
var isNewVersion = versionInfo.resourceInfo.isNewVersion;
var indexUrl = versionInfo.baseUrl + versionInfo.resourceInfo.version + versionInfo.resourceInfo.indexURL;
if(versionInfo.resourceInfo && versionInfo.resourceInfo.indexURL){
if (mode === "1") {
if (isNewVersion) {
navigator.appUtils.updateAppResource(resourceDownlaodUrl).done(function() {
navigator.appUtils.setIndexPageUrl(indexUrl);
});
} else {
navigator.appUtils.setIndexPageUrl(indexUrl);
}
} else if (mode === "2") {
navigator.appUtils.setIndexPageUrl(indexUrl);
navigator.appUtils.updateAppResource(resourceDownlaodUrl);
} else if (mode === "3") {
navigator.appUtils.setIndexPageUrl(indexUrl);
}
}
}
} catch (updateError) {
}
};
VersionChecker.prototype.checkAppVersion = function() {
var self = this;
if (cordova && cordova.getAppVersion) {
cordova.getAppVersion.getVersionNumber(function(currentVersion) {
if (self.appInfo[self.env] && currentVersion < self.appInfo[self.env].version && window.localStorage && localStorage.getItem('versionIgnore') !== self.appInfo[self.env].version) {
self.versionMsg.show({
type : "YesNoCancel",
title : "版本更新",
message : self.appInfo[self.env].changeLog
});
}
});
}
};
VersionChecker.prototype.download = function() {
if (Browser.isAndroid && cordova.plugins.fileOpener2) {
this.downLoadApp();
}else{
window.open(this.appInfo[this.env].downloadURL, '_system');
}
};
VersionChecker.prototype.ignore = function() {
if (window.localStorage) {
localStorage.setItem('versionIgnore', this.appInfo[this.env].version);
}
};
VersionChecker.prototype.downLoadApp = function(event){
justep.Util.hint("开始下载,完成将自动运行安装程序.");
var me = this;
//var divProgress = $(".cssprocessbar"); //获取当前行的进度条
var filePath = 'file:///storage/emulated/0/tmp/app'+this.appInfo[this.env].version+'.apk'; //下载路径,数据库保存文件名
var uri = encodeURI(this.appInfo[this.env].downloadURL); //下载地址
var fileTransfer = new FileTransfer();
//fileTransfer.onprogress = function(progressEvent) { //文件传输插件对象的进度方法调用
// var percentProgress = parseInt((progressEvent.loaded / progressEvent.total) * 100)+ "%"; //获取已下载和总大小的百分比
// if (progressEvent.lengthComputable)//获取浏览器返回HEAD信息包含CONTENT-LENGTH为TRUE 则执行下面
// {
// divProgress.css("width",percentProgress); //设置进度条的样式
// divProgress.html(percentProgress); //设置进度条里面的数字
// }
//};
fileTransfer.download( //调用对象的下载方法,开始下载
uri,
filePath,
function(entry) {
console.log("download complete: " + entry.fullPath);
me.openFile();
},
function(error) { //出错回调函数
console.log("download error source " + error.source);
console.log("download error target " + error.target);
console.log("upload error code" + error.code);
},
false,
{
headers: {
}
}
);
};
VersionChecker.prototype.openFile = function(event){
var url = require.toUrl('file:///storage/emulated/0/tmp/app'+this.appInfo[this.env].version+'.apk');
cordova.plugins.fileOpener2.open(url, "application/vnd.android.package-archive", {
error : function() {
justep.Util.hint("打开应用失败!");
},
success : function() {
justep.Util.hint("下载成功,即将关闭APP进行更新!");
navigator.app.exitApp();
}
});
};
return new VersionChecker();
}); |
|