Foreword
Recently, there are many interactions with WebView. WebView is a browser control in Android. Here we mainly introduce how WebView is loaded to the WebViewClient class to control URL loading.
Use WebViewClient
Using WebViewClinet mainly inherits the WebViewClient class, rewrite the method as needed, and configure it in WebView. The example code is as follows:
webview = (webView) FindViewByid (R.id.Webview); webview.setWebViewClient (New ExamplewebViewClient ()); Private class Extends WebViewClient {@Override Public Void ON careivedssler (WebView View, SSLERRORHANDLER, SSLERROR ERROR) {Handler.prceed ( );} @Override Public Boolean Shoulriderideurlloading (WebView View, String URL) {View.loadurl (url); RETURN TRUE; d onPageFinISHED (webview view, string url) {super.onpagefinished (view, url);} @Override Public Void OnPageStarted ONLOADRSOURCE (WebView View, String URL) {Super.OnloadResource (view, url); }}
WebViewClient method
1. Shouldoverrideurlloading (WebView View, 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 proved, B. Y 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. d is not called for requests using the post "method".
Translation: When a new URL is to load the current webview, this method gives the application an opportunity to control the processing of URL. If webview does not have SetwebViewClient, the default operation is that webview will ask Activity Manager to get the right handler processing URL. If WebView sets SetwebViewClient, returns True represents the current application to process URL, and returns FALSE to represent the current WebView to handle URL. If the HTTP request is the POST method, the method will not be called.
Code example:
/ *** All URL calls the system browser that starts with www.example.com to open other URLs on the current webview. if (url.indexof ("http: //www.example.com ")! = -1) {// Call the system by default browser processing url view.stoploading (); view.getContext (). se (url )); Return true;} Return false;}
2. Shouleoverridekeyevent (WebView View, Keyevent Event)
Official note: Give The host application a change to handle the key the key the key, event synchronly. Will not handle the key event. If Return false, webview will alway the key EVENT, so none of the super in the view chain will see the key event. The default behavior return false.
Translation: Give the current application of an opportunity to handle key events asynchronously. Returning True, webview will not handle the key event, return false, webview will process the key event. The default return is false.
3. OnPageStarted (WebView View, String URL, Bitmap Favicon) and OnPageFinished (WebView View, String URL)
Official note: Notify the Host Application that a Page Has Started Loading. This Method is Called once Main Frame Load SO A Page with IframeSets Will CA LL OnPageStarted One Time for the main frame. This Also Means that OnPageStarted Will Not Be Called When the contents of an emBedded Frame Changes, IE Clicing A Link Whose Target is an iframe.
Translation: It is called when the page starts loading. However, when the page is nested (such as a link jump in iframe), this method will not be called. (This is the case today, you can control the URL jump by re -loading onLoadResource)
Official note: Notify the Host Application that a Page Has Finished Loading. This Method is Called only only for mail. What rendering re May be updated yet. To get the notification for the new picture, use onnewpicture (WebView, Piction).
Translation: It was called at the end of the page load.
Code example:
// Get the page load time
Private Long Starttime; Private Long Endtime; Private Long Spendtime; @Override Public Void OnPageFinished (WebView View, String URL) {endtime = Sy. STEM.CURRENTTTTIMEMILLIS (); 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) {Startti me = System.CurrenttiMemillis ();}
4. OnlineRsource (WebView View, String URL)
Official note: Notify the Host Application that the WebView Will Load the Resource Specified by the Given URL.
Translation: Notify the application WebView to load the specified URL resources, each resource (such as pictures, nested URL, JS, CSS files). (You can handle the IFRAME nested URL through this method)
Code example:
@Override Public Void Ondresource (WebView View, String URL) {if (url.indexof ("http://www.example.com")! = -1 && View! = NULL) plading (); view. getContext (). StartActivity