Method of using webviewclient to process jump URL in Android system

Foreword recently, there are many interactions with WebView in the code. WebView is a browser control in Android. Here we mainly introduce how WebView overloads webviewclient class to control URL loading.

Using webviewclient using webviewclinet mainly inherits the webviewclient parent class, rewrites the methods as needed, and configures them in WebView. The example code is as follows:

Webviewclient method 1. Shouldoverrideurlloading (WebView, string URL)

Official note: 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".

When a new URL is to be loaded in the current WebView, this method gives the application an opportunity to control the processing of the URL. If WebView does not have setwebviewclient, the default operation is that WebView will ask the activity manager for the appropriate handler processing URL. If setwebviewclient is set for WebView, returning true means that the current application processes the URL, and returning false means that the current WebView processes the URL. If the HTTP request is a post method, the method will not be called. Code example:

2. shouleOverrideKeyEvent(WebView view,KeyEvent event)

Official notes: 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.

Give the current application an opportunity to handle key events asynchronously. If true is returned, WebView will not process the key event. If false is returned, WebView will process the key event. The default return is false. 3. Onpagestarted (WebView, bitmap favicon) and onpagefinished (WebView, string URL)

Official note: notify the host application that a page has started loading. This method is called once for each main frame load so a page with frames or frames 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.

Called when the page starts loading. However, when the page is nested (for example, there is a link jump in iframe), this method will not be called. (this is the case today. You can overload onloadresource to control URL jump)

Official note: 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)

Called at the end of page loading. Code example:

//Get page load time

4. onLoadResource(WebView view,String url)

Official note: notify the host application that the WebView will load the resource specified by the given URL

Inform the application that WebView will load the resources of the specified URL, and each resource (such as image, nested URL, JS, CSS file). (you can handle iframe nested URLs through this method) code example:

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>