Use the WebViewClient method to handle jump urls in the Android system

  • 2020-04-01 04:07:41
  • OfStack

preface
Recently, there has been a lot of interaction with WebView in the code. WebView is a browser control in android. Here, I mainly introduce how WebView reloads the WebViewClient class to control URL loading.

Use the WebViewClient
The main purpose of using WebViewClinet is to inherit the WebViewClient parent class, rewrite the method according to the need, and configure it in WebView. The sample code is as follows:

     


 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 method
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 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". 

This method gives the application a chance to control the handling of a new url when it is loaded in the current WebView. If WebView does not have a setWebViewClient, the default action is that WebView will ask the Activity Manager for the appropriate handler to handle the url. If the WebView is set to setWebViewClient, return true for the current application to process the url, and return false for the current WebView to process the url. If the HTTP request is a POST method, the method will not be called.
Code example:

 


   
  @Override 
  public boolean shouldOverrideUrlLoading(WebView view, String url) { 
    if (url.indexOf("http://www.example.com") != -1) { 
      //Call the system's default browser to handle 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 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.

Give the current application an opportunity to handle key events asynchronously. Return true, WebView will not handle the key event, return 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 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. for clicking a link whose target is an iframe.  

Called when the page starts to load. However, this method is not called when the page is nested (for example, there is a link jump in an iframe). (as we saw today, you can control the url jump by reloading onLoadResource.)

      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 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 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.

Notifies the application that the WebView will load the specified url of the resource, each resource (such as the image, nested url, js, CSS file). (you can handle iframe nested urls in this way)
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 articles: