How to use Webviewclient to process jump URLs in the Android system _java

Source: Internet
Author: User

Objective
There's a lot of interaction with WebView in the recent code, WebView is the browser control in Android, and here's how WebView overloads the Webviewclient class to control URL loading.

Using Webviewclient
using Webviewclinet is primarily to inherit the Webviewclient parent class, rewrite the methods as needed, and configure them in WebView, as shown in the sample code:

 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.onpagesta 
    rted (view, URL, favicon); 
    @Override public 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 "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, 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 applies an opportunity to control the processing of the URL. If WebView does not have setwebviewclient, the default action is WebView will ask the activity manager for the appropriate handler processing URL. If WebView sets Setwebviewclient, returning true represents the current application to process the URL, and returning false represents the current WebView to process the URL. If the HTTP request is a POST method, the method will not be invoked.
code example:

  /** 
   * All URLs beginning with www.example.com call the system browser to open the other URL at the current WebView Open/ 
  @Override public 
  Boolean Shouldoverrideurlloading (webview view, String URL) { 
    if (Url.indexof ("http://www.example.com")!=-1) { 
      // Invokes the system default browser to process the URL 
      view.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 way. If return True, WebView won't handle the key event. If return False, WebView'll always handle the key event, so none of the super in the view chain'll to 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 handle 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 that a page has started loading. This are called once for each main frame load so a page with IFRAMEs or framesets'll call onpagestarted one time F or the main frame. This is also means that onpagestarted won't be called the contents of a embedded frame changes, i.e. clicking a link Whose target is an iframe.

Translation: Called when the page starts to load. However, the method will not be invoked when the page is nested (for example, there is a link jump in the iframe). (This is the case today, you can control the URL jump by overloading the Onloadresource)

Official note: Notify the host application that a page has finished loading. This was called only the 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 the page load.
code example:

Get page load time

 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 are:" + spendtime, Toast.length_short). Show (); 
   
  @Override public 
  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 WebView the resource that will load the specified URL, each resource (such as a picture, nested Url,js,css file). (You can handle an iframe nested URL by this method)
code example:

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

Related Article

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.