Android实现WebView点击拦截跳转原生
作者:qq_41880285 时间:2023-01-06 23:47:38
1. 首先设置Web视图
webview.setWebViewClient(new MyWebViewClient());
webview1.setWebViewClient(new MyWebViewClient());
2. 拦截点击的链接,跳转到对应的页面
// 监听 所有点击的链接,如果拦截到我们需要的,就跳转到相对应的页面。
private class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.e("tsg","url==="+url);
//这里进行url拦截
if (url != null && url.contains(https://www.jb51.net)) {
字符串截取需要的
String str="";
str=url.substring(url.lastIndexOf("id/"),url.length());
str=str.substring(3,str.indexOf("."));
Log.e("tsg","id==="+str);
// Toast.makeText(mActivity, "njhnuj", Toast.LENGTH_SHORT).show();
//跳转需要跳转的页面
Intent intent = new Intent();
intent = new Intent(getActivity(), SPProductDetailActivity.class);
intent.putExtra("goodsID",str);
getActivity().startActivity(intent);
return true;
}
return super.shouldOverrideUrlLoading(view, url);
}
@Override
public void onPageFinished(WebView view, String url) {
view.getSettings().setJavaScriptEnabled(true);
super.onPageFinished(view, url);
}
}
补充知识:Android9.0 WebView空白页面
最近遇见线上客户部分手机9.0系统,在使用到webview的页面,都是空白,无法正常加载出页面。。。
原因是:
在安卓9.0版本中有一个新特性如下
所有应用都使用HTTPS
这是对Android安全用户的肯定。AndroidPie上的所有应用程序默认都使用HTTPS。Android关心您的隐私。
也就是说从Android 9.0(API级别28)开始,默认情况下禁用明文支持。因此http的url均无法在webview中加载。
解决办法:
在AndroidManifest.xml文件中的APplication标签添加android:usesCleartextTraffic="true"。
android:usesCleartextTraffic="true"
来源:https://blog.csdn.net/qq_41880285/article/details/90437561
标签:Android,WebView,拦截,原生
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
SpringBoot使用过滤器、拦截器和监听器的案例代码(Springboot搭建java项目)
2022-11-21 06:08:54
Android Drawerlayout实现侧滑菜单效果
2022-10-31 19:39:23
java数据结构算法稀疏数组示例详解
2021-09-30 11:47:28
![](https://img.aspxhome.com/file/2023/6/67956_0s.png)
Android中自定义ImageView添加文字设置按下效果详解
2021-10-08 08:13:14
![](https://img.aspxhome.com/file/2023/7/130297_0s.gif)
基于C#制作一个休息提醒闹钟的详细步骤
2022-01-23 06:14:38
![](https://img.aspxhome.com/file/2023/9/81059_0s.png)
Android中通知Notification使用实例(振动、灯光、声音)
2021-09-28 20:00:01
![](https://img.aspxhome.com/file/2023/3/109183_0s.jpg)
C语言中求字符串长度的函数的几种实现方法
2023-07-04 23:29:05
Java中this,static,final,const用法详解
2022-01-23 17:21:41
Android 使用Shell脚本截屏并自动传到电脑上
2022-07-06 01:21:43
Android中 动态改变对话框值的方法
2023-08-17 19:44:30
C#类的多态性详解
2022-04-27 02:55:42
![](https://img.aspxhome.com/file/2023/5/79115_0s.png)
Android自定义状态栏颜色与应用标题栏颜色一致
2022-01-12 02:24:31
![](https://img.aspxhome.com/file/2023/3/102573_0s.png)
Java多线程之条件对象Condition
2021-09-25 15:15:30
![](https://img.aspxhome.com/file/2023/7/79837_0s.png)
新手必看Unity2019 2020保姆级安装教程
2023-08-23 07:13:37
![](https://img.aspxhome.com/file/2023/3/102153_0s.png)
C#实现自定义动画鼠标的示例详解
2021-07-31 17:16:59
![](https://img.aspxhome.com/file/2023/4/106424_0s.png)
C# 面向对象三大特性:封装、继承、多态
2022-09-15 17:00:53
unity实现翻页按钮功能
2021-07-15 03:33:47
![](https://img.aspxhome.com/file/2023/4/113684_0s.jpg)
Java利用三目运算符比较三个数字的大小
2023-04-18 01:20:19
Android 设置Edittext获取焦点并弹出软键盘
2022-02-28 12:02:02
Android动画之补间动画(Tween Animation)实例详解
2023-10-18 09:40:53
![](https://img.aspxhome.com/file/2023/1/88101_0s.png)