代码拉取完成,页面将自动刷新
抛弃使用高风险的WebView addJavascriptInterface方法,通过对js层调用函数及回调函数的包装,支持异步回调,方法参数支持js所有已知的类型,包括number、string、boolean、object、function。 同时还针对WebView的一些常用的方法进行了一定的封装,像返回、刷新、网页中图片保存、是否用系统浏览器进行打开
使用 CustomWebViewBridge 最简单的办法就是像下面这样添加项目依赖。
allprojects {
repositories {
maven {url'https://jitpack.io' }
}
}
dependencies {
implementation 'com.gitee.quanguanzhou:CustomWebViewBridge:v2.0'
}
<!-- <repositories> section of pom.xml -->
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
<!-- <dependencies> section of pom.xml -->
<dependency>
<groupId>com.gitee.quanguanzhou</groupId>
<artifactId>CustomWebViewBridge</artifactId>
<version>v1.0</version>
</dependency>
CustomWebView.Builder builder = new CustomWebView.Builder(this, (ViewGroup) contentView);
builder.setPageCenterTitle(centerTitle).setBackColor(backColor);
builder.setBackPic(backPic).setBackFontColor(backfontColor);
builder.setCloseFontColor(closeFontColor).setCenterFontColor(centerFontColor);
builder.setRightFontColor(rightFontColor).setUrl(url);
builder.setIsShowBack(isShowBack).setIsShowClose(isShowClose).setIsRefresh(isRefresh);
builder.setNavigationCallbackListener(new OnNavigationCallbackListener() {
@Override
public void doLeftbackListener(WebView webView, Activity activity) {
// 已经处理返回的方法,如果网页中有历史页面可以返回,那就返回历史页面,如果没有就返回到前一Activity
super.doLeftbackListener(webView, activity);
}
@Override
public void doLeftCloseListener(WebView webView, Activity activity) { //关闭
super.doLeftCloseListener(webView, activity);
}
@Override
public void doRightCallbackListener(WebView webView, Activity activity) {// 刷新页面
super.doRightCallbackListener(webView, activity);
}
});
builder.setHostJsScopeClass(CustomHostJsScope.class); // 设置自定义的Js与Java的交互处理类
builder.create();
/**
* 监听用户手机按键,如果打开的网址可以返回,
* 则返回,如果不可以返回则正常处理
* @param keyCode
* @param event
* @return
*/
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
WebView webView = builder.getWebView();
if(webView!=null) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && builder.getWebView().canGoBack()) {
builder.getWebView().goBack();
return true;
}
}
return super.onKeyDown(keyCode, event);
}
@Override
protected void onDestroy() {
super.onDestroy();
builder.onDestroy();
}
Activity activity;
String isShowBack; //是否显示返回 1是0否
String isShowClose;//是否显示关闭 1是0否
String isRefresh;//是否显示刷新 1是0否
/**
* 需要显示的Url,可以是:file:///android_asset/、可以是Http、Https网址
*/
String url;
/**
* 返回的图片
*/
int backPic;
/**
* 设置返回的的字体颜色
*/
int backfontColor;
/**
* 设置关闭的字体颜色
*/
int closeFontColor;
/**
* 设置标题的字体颜色
*/
int centerFontColor;
/**
* 设置右边的字体颜色
*/
int rightFontColor;
/**
* 设置右边的文字
*/
String rightValue;
/**
* 设置右边文字左边的图标
*/
int rightLeftPicResId;
/**
* 导航条的背景颜色
*/
int backColor;
/**
* WebView中的连接是否用系统浏览器打开
* 默认不用系统浏览器打开
*/
String openBySystem = "0";
/**
* 是否启用Js,默认为启用
*/
boolean javaScriptEnabled = true;
/**
* 缓存模式,默认为:WebSettings.LOAD_NO_CACHE;
*/
int cacheMode = WebSettings.LOAD_NO_CACHE;
/**
* 页面标题
*/
String centerTitle;
/**
* CustomWebView,返回、关闭、刷新的自定义点击事件
*/
OnNavigationCallbackListener onNavigationCallbackListener;
/**
* DOM储存
*/
boolean domStorageEnabled = true;
Class<? extends HostJsScope> hostJsScopeClass;
参考:(pedant) 的safe-java-js-webview-bridge库
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。