Android webviewclient processing Jump URL

Source: Internet
Author: User

Preface the recent code and WebView have a lot of interaction, WebView is the browser control in Android, here is the main introduction WebView How to overload webviewclient class to control URL loading.
Using Webviewclient to use webviewclinet is primarily to inherit the Webviewclient parent class, override the method as needed, and configure it in WebView, with the sample code as follows:
WebView = (WebView) Findviewbyid (R.id.webview), Webview.setwebviewclient (New Examplewebviewclient ());p Rivate class Examplewebviewclient extends Webviewclient {@Overridepublic void Onreceivedsslerror (WebView view, Sslerrorhandler Handler, sslerror error) {handler.proceed ();} @Overridepublic Boolean shouldoverrideurlloading (WebView view, String URL) {view.loadurl (URL); return true;} @Overridepublic void onpagefinished (WebView view, String URL) {super.onpagefinished (view, URL);} @Overridepublic void onpagestarted (WebView view, String URL, Bitmap favicon) {super.onpagestarted (view, URL, favicon);} @Overridepublic void Onloadresource (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 was about to being loaded in the current WEBVI ew. If webviewclient is not provided,by default WebView would ask Activity Manager to choose the proper handler for the URL.  If Webviewclient is provided, return true means the host application handles the URL and while return false means the current WebView handles the URL. This method isn't called for requests using the POST "method".
Translation: When a new URL is to be loaded at the current WebView, this method gives the application a chance to control the processing of the URL. If WebView does not have setwebviewclient, the default action is WebView will ask activity manager for the appropriate handler processing URL. If WebView is set to Setwebviewclient, returns true to represent the current app to process the URL, and false to represent the current webview to process the URL. If the HTTP request is a POST method, the method will not be called. code example:
/** * All URLs that start with www.example.com call the system browser to open the other URLs in the current WebView Open/@Overridepublic boolean shouldoverrideurlloading ( WebView view, String URL) {if (Url.indexof ("http://www.example.com")! =-1) {//Call system default browser to process urlview.stoploading (); View.getcontext (). StartActivity (New Intent (Intent.action_view, Uri.parse (URL))); return true;} return false;}
2. Shouleoverridekeyevent (WebView view, KeyEvent event)
Official note: Give the host application a chance to handle the key event synchronously. e.g. menu shortcut key events need to is filtered this. If return True, WebView would not handle the key event. If return False, WebView'll always handle the key event and so none of the super in the view chain would see the key event. The default behavior returns FALSE.
Translation: gives the current application an opportunity to handle key events asynchronously. Returning True,webview will not process the key event, and returning False,webview will handle 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 A page has started loading. This method was called once for each main frame load so a page with IFRAMEs or framesets would call onpagestarted one time F or the main frame.  This also means that onpagestarted won't be called when the contents of a embedded frame changes, i.e. clicking a link Whose target is an iframe.
Translation: Called when the page starts loading. However, the method will not be called when the page is nested (for example, there is a link jump in the iframe). (This is the case today, you can control URL jumps by overloading Onloadresource)
Official note: Notify the host application A page has finished loading. This method was called only for main frame. When Onpagefinished () was called, the rendering picture could be updated yet. To get the notification for the new picture, use Onnewpicture (WebView, picture).
Translation: Called at the end of page loading. code example:
Gets the page load time private long starttime;private long endtime;private long spendtime; @Overridepublic void onpagefinished ( WebView view, String url) {endTime = System.currenttimemillis (); spendtime = Endtime-starttime; Toast.maketext (View.getcontext (), "Spend time is:" + Spendtime, Toast.length_short). Show (); @Overridepublic void onpagestarted (WebView view, String URL, Bitmap favicon) {startTime = System.currenttimemillis ();}
4. Onloadresource (WebView view, String URL)
Official note: Notify the host application that the WebView would load the resource specified by the given URL.
Translation: Notifies the application that WebView will load the resource of the specified URL, each resource (a sample, nested Url,js,css file). (You can use this method to handle the URL of an IFRAME nesting) code example:
@Overridepublic 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 )));}}


Android webviewclient processing Jump URL

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.