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


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。