在Android系统中使用WebViewClient处理跳转URL的方法
作者:低调小一 发布时间:2021-08-03 14:24:59
前言
最近代码里和WebView有很多的交互,webview是android中的浏览器控件,这里主要介绍一下webview如何重载WebViewClient类来控制URL加载。
使用WebViewClient
使用WebViewClinet主要是继承WebViewClient父类,根据需要重写其中的方法,并在WebView中进行配置,示例代码如下:
webView = (WebView) findViewById(R.id.webview);
webView.setWebViewClient(new ExampleWebViewClient());
private class ExampleWebViewClient extends WebViewClient {
@Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
handler.proceed();
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
@Override
public void onLoadResource(WebView view, String url) {
super.onLoadResource(view, url);
}
}
WebViewClient方法
1. shouldOverrideUrlLoading(WebView view, String url)
官方注释:Give the host application a chance to take over the control when a new url is about to be loaded in the current WebView. If WebViewClient is not provided,by default WebView will ask Activity Manager to choose the proper handler for the url. If WebViewClient is provided, return true means the host application handles the url, while return false means the current WebView handles the url. This method is not called for requests using the POST "method".
翻译:当一个新的url要在当前WebView进行加载的时候,这个方法给应用一个机会来控制url的处理。如果WebView没有setWebViewClient,则默认操作是WebView将询问Activity Manager获取合适的handler处理url。如果WebView设置了setWebViewClient,返回true代表当前应用来处理url,返回false则代表当前webview来处理url。如果http请求是POST方法,该方法将不会被调用。
代码示例:
/**
* 所有以www.example.com开头的url调用系统浏览器打开 其他的url在当前webview打开
*/
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.indexOf("http://www.example.com") != -1) {
// 调用系统默认浏览器处理url
view.stopLoading();
view.getContext().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
return true;
}
return false;
}
2. shouleOverrideKeyEvent(WebView view, KeyEvent event)
官方注释:Give the host application a chance to handle the key event synchronously. e.g. menu shortcut key events need to be filtered this way. If return true, WebView will not handle the key event. If return false, WebView will always handle the key event, so none of the super in the view chain will see the key event. The default behavior returns false.
翻译:给当前应用一个机会来异步处理按键事件。返回true,WebView将不会处理该按键事件,返回false,WebView将处理该按键事件。默认返回是false。
3. onPageStarted(WebView view, String url, Bitmap favicon)和onPageFinished(WebView view, String url)
官方注释:Notify the host application that a page has started loading. This method is called once for each main frame load so a page with iframes or framesets will call onPageStarted one time for the main frame. This also means that onPageStarted will not be called when the contents of an embedded frame changes, i.e. clicking a link whose target is an iframe.
翻译:当页面开始加载时被调用。但是,当页面被嵌套时(例如iframe里有一个链接跳转),该方法将不会被调用。(今天就遇到了这种情况,可以通过重载onLoadResource来控制url跳转)
官方注释:Notify the host application that a page has finished loading. This method is called only for main frame. When onPageFinished() is called, the rendering picture may not be updated yet. To get the notification for the new Picture, use onNewPicture(WebView, Picture).
翻译:在页面加载结束时被调用。
代码示例:
// 获取页面加载时间
private long startTime;
private long endTime;
private long spendTime;
@Override
public void onPageFinished(WebView view, String url) {
endTime = System.currentTimeMillis();
spendTime = endTime - startTime;
Toast.makeText(view.getContext(), "spend time is:" + spendTime, Toast.LENGTH_SHORT).show();
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
startTime = System.currentTimeMillis();
}
4. onLoadResource(WebView view, String url)
官方注释:Notify the host application that the WebView will load the resource specified by the given url.
翻译:通知应用程序WebView将要加载指定url的资源,每一个资源(例如图片,嵌套url,js,css文件)。(可以通过该方法处理iframe嵌套的url)
代码示例:
@Override
public void onLoadResource(WebView view, String url) {
if (url.indexOf("http://www.example.com") != -1 && view != null) {
view.stopLoading();
view.getContext().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
}
}


猜你喜欢
- 公平锁,顾名思义,它是公平的,可以保证获取锁的线程按照先来后到的顺序,获取到锁。非公平锁,顾名思义,各个线程获取到锁的顺序,不一定和它们申请
- 类加载所有类加载器,都是ClassLoader的子类。类加载器永远以.class运行的目录为准。读取classpath根目录下的文件有以下几
- 写在前面:spring 应该对于每个从事java开发的大兄弟们来说应该都不陌生的,作为一个从业两年多的小开发仔,个人觉得,每天都在面对spr
- Lombok有什么用在我们实体Bean中有大量的Getter/Setter方法以及toString, hashCode等可能不会用到,但是某
- Spring注入方式可以分为三类,xml注入、注解注入、BeanDefinition注入;用法上可以分为三种,但是底层实现代码都是统一Bea
- 基于IntelliJ Platform Plugin搭建环境步骤File->New->Project选择IntelliJ Pla
- POM:<dependency> <groupId>com.baomidou</groupId&g
- 本文实例讲述了Android编程自定义线程池与用法。分享给大家供大家参考,具体如下:一、概述:1、因为线程池是固定不变的,所以使用了单例模式
- 一、引言在软件开发过程中,我们经常会遇到处理简单对象和复合对象的情况,例如对操作系统中目录的处理就是这样的一个例子,因为目录可以包括单独的文
- 输入方法第一种输入方法:scannerimport java.util.Scanner; // 导入java.util.Scannerpub
- 本文实例讲述了C#使用IComparer自定义List类实现排序的方法。分享给大家供大家参考。具体如下:List类中不带参数的Sort函数可
- 本文实例为大家介绍了几个可用的类,供大家参考,具体内容如下1.SQLHelper类using System;using System.Col
- 一、算术运算符运算符:常见的±*/等表达式:运算符连接起来符合java语法的式子,比如a+b算术运算符:+ - * / %注:要想得到小数,
- Spring @Async无法实现异步问题原因项目中存在2个配置文件:springMVC.xml和beanDefines.xml,它们都配置
- 本文实例讲述了java识别一篇文章中某单词出现个数的方法。分享给大家供大家参考。具体如下:1. java代码:import java.io.
- Java读取txt文件内容。可以作如下理解:首先获得一个文件句柄。File file = new File(); file即为文件句柄。两人
- 本文研究的主要是java fastdfs客户端使用实例的相关内容,具体实现如下。什么是FastDFS?FastDFS是用c语言编写的一款开源
- 基于SMTP发送一个简单的邮件首先,需要一个认证器:package No001_基于SMTP的文本邮件;import javax.mail.
- 投影(Projection) 是一种可以将查询结果进行 塑性 的一种操作,你可以使用 投影 将一个 object 转成仅包含你需要属性的新对
- 目录(1)class常量池(2)运行时常量池(3)基本类型包装类常量池(4)字符串常量池总结java中有几种不同的常量池,以下的内容是对ja