在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))); } }