当前位置:网站首页>webView基本使用
webView基本使用
2022-07-27 14:55:00 【CAir2】
- 启用JavaScript
WebSettings settings = mWebView.getSettings();
settings.setJavaScriptEnabled(true);
//settings.setAllowFileAccess(true);
//settings.setAllowFileAccessFromFileURLs(true);
//settings.setAllowContentAccess(true);
settings.setDatabaseEnabled(true);
settings.setDomStorageEnabled(true);
- 拦截加载过程中指定的url或者广告,重写
WebViewClient::shouldInterceptRequest
mWebView.setWebViewClient(new WebViewClient(){
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
if(mRefreshLayout != null){
mRefreshLayout.finishRefresh();
}
}
@Nullable
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
Uri uri = request.getUrl();
//过滤基本的广告url和替换图片
if(uri.toString().equals("https://xxx.png")){
try {
InputStream localCopy = getAssets().open("empty.png");
return new WebResourceResponse("image/png", "UTF-8", localCopy);
}
catch (Exception e){
return new WebResourceResponse(null,null,null);
}
}
if (isAds(uri)){
return new WebResourceResponse(null,null,null);
}
return super.shouldInterceptRequest(view, request);
}
@Override
public void onLoadResource(WebView view, String url) {
super.onLoadResource(view, url);
Log.i("tUrl", url);
}
});
其中Assert的目录和java,res同级

- 监控url加载进度,重写
WebChromeClient::onProgressChanged
mWebView.setWebChromeClient(new WebChromeClient(){
@Override
public void onProgressChanged(WebView view, int newProgress) {
super.onProgressChanged(view, newProgress);
}
});
- 拦截href跳转,重写
WebViewClient::shouldOverrideUrlLoading
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
if (request.getUrl().toString().equals("https://xxx")){
//取消当前链接
return true;
}
return super.shouldOverrideUrlLoading(view, request);
}
边栏推荐
猜你喜欢

C语言之字符函数和字符串函数及内存函数

C语言之函数

C语言之指针进阶

MPC_ ORCA

CDQ divide and conquer and whole dichotomy learning notes

Storage of data in C language

Automatic classification of e-commerce UGC pictures using Baidu PaddlePaddle easydl

Kubesphere multi node installation error

JDBC连接数据库

2021 national vocational college skills competition (secondary vocational group) network security competition questions (9) ideas
随机推荐
C语言之操作符
C language output string in reverse order
牛客题目——判断是不是完全二叉树、平衡二叉树
mvc和mvp和mvvm的区别
The difference between MVC and MVP and MVVM
C语言之数据的储存
Random number formula random
Layoff quarrel, musk: I'm too hard; Mercilessly open source a public opinion acquisition project; Feature engineering is as simple as parameter adjustment?! Nerf boss shouted that he couldn't move; Cu
js中的函数
Start from scratch blazor server (1) -- project construction
Matplotlib drawing error: "! Latex error: file `type1cm.sty 'not found." solution
Jerry's in ear detection function [chapter]
Quadratic programming based on osqp
mysql视图及存储过程
牛客题目——最小的K个数
String numeric type converted to thousands
As changes the background theme and background picture
Configuration and application of gurobi in pycharm
Jerry's maximum volume prompt sound cannot be broadcast [article]
md 中超链接的解析问题:解析`this.$set()`,`$`前要加空格或转义符 `\`