在Android系统中使用WebViewClient处理跳转URL的方法
前言
最近代码里和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)));
}
}
相关文章
XML Web 服务 Eclipse实现sun-jaxws.xml文件的方法
在sun-jaxws.xml文件,可以配置endpoint、handler-chain等内容,在这个文件中配置的内容会覆盖在Java代码中使用注解属性配置的的内容,本文给大家介绍的非常详细,感兴趣的朋友一起看看吧2023-11-11
SpringBoot @ExceptionHandler与@ControllerAdvice异常处理详解
在Spring Boot应用的开发中,不管是对底层数据库操作,对业务层操作,还是对控制层操作,都会不可避免的遇到各种可预知的,不可预知的异常需要处理,如果每个处理过程都单独处理异常,那么系统的代码耦合度会很高,工作量大且不好统一,以后维护的工作量也很大2022-10-10
避免多个jar通过maven打包导致同名配置文件覆盖冲突问题
这篇文章主要介绍了避免多个jar通过maven打包导致同名配置文件覆盖冲突问题,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪2023-05-05


最新评论