@DCloud_Android_ST
用的就是20181226随SDK附带的HBuilder-Hello项目的index.html页面,在原来的pluseReady方法末尾添加延时创建新Webview的方法。注意确保延时打开的代码是在APP切到后台后执行,后台运行创建问题复现率100%,创建的时候APP在前台,就没有这样的问题。
// H5 plus事件处理
function plusReady(){
// Android处理返回键
plus.key.addEventListener('backbutton',function(){
('iOS'==plus.os.name)?plus.nativeUI.confirm('确认退出?', function(e){
if(e.index>0){
plus.runtime.quit();
}
}, 'HelloH5', ['取消','确定']):(confirm('确认退出?')&&plus.runtime.quit());
},false);
// 关闭启动界面
plus.navigator.setStatusBarBackground('#D74B28');
setTimeout(function(){
plus.navigator.closeSplashscreen();
},200);
// 切后台后打开新的webview,延时时间自己调整,只要确保执行的时候App是在后台运行的
setTimeout(function() {
behindOpen();
}, 10000);
}
创建新Webview的方法
function behindOpen() {
// 新建Webview的Style
var webviewAppendJsFiles = [];
var overrideResourceOptions =[];
var commonWebveiwStyle = {
render: "always",
kernel: "WKWebview", // webview内核
progress: { // 加载进度条
color: "#03a9f4"
},
userSelect: false, // 是否可以长按选择页面文本,
statusColor: "#2196f3",
animationTypeShow: "pop-in"
}
var externalSrc = "http://www.baidu.com/sdsads";
var statusColor = "#2196f3";
var animationTypeShow = "fade-in";
var backgroundColor = "transparent";
var mask = "rgba(0,0,0,0.4)";
var contentAdjust = true;
if ('iOS'==plus.os.name) {
statusColor = "#f7f7f8";
backgroundColor = "#f7f7f8";
animationTypeShow = "pop-in";
mask = "none";
contentAdjust = false;
}
// iOS 需要制定kernel内核为WKWebview
var externalWebviewStyle = Object.assign({}, commonWebveiwStyle, {
mask: mask, // 遮罩层
background: backgroundColor, // 窗体背景
statusbar: { // 状态栏样式
background: statusColor
},
// 无效属性// contentAdjust: contentAdjust, // iOS11及以上版本,沉浸式状态栏存在WebView顶部状态栏灰条的问题,增加contentAdjust:false 可以取消自动调整(true:内容区域避免和安全区域重叠,主要是为刘海屏作出的调整)
});
var externalWebview = plus.webview.create(externalSrc, "externalWebview", externalWebviewStyle);
// 向webview添加本地JS文件,按先后顺序执行
for(var i in webviewAppendJsFiles) {
var appendJsFile = webviewAppendJsFiles[i];
externalWebview.appendJsFile(appendJsFile);
}
externalWebview.overrideResourceRequest(overrideResourceOptions);
// 监听页面加载完成
externalWebview.addEventListener('loaded', function(e){
plus.webview.getTopWebview().setStyle({
mask: 'none'
});
plus.nativeUI.closeWaiting();
}, false);
externalWebview.show(animationTypeShow);
plus.nativeUI.showWaiting(waitingTitle, waitingOptions);
}