在Android系统中使用WebViewClient处理跳转URL的方法

 更新时间:2015年07月31日 14:52:30   作者:低调小一  
这篇文章主要介绍了在Android系统中使用WebViewClient处理跳转URL的方法,实现代码为Java语言编写,是需要的朋友可以参考下

前言
最近代码里和WebView有很多的交互,webview是android中的浏览器控件,这里主要介绍一下webview如何重载WebViewClient类来控制URL加载。

使用WebViewClient
使用WebViewClinet主要是继承WebViewClient父类,根据需要重写其中的方法,并在WebView中进行配置,示例代码如下:

   

 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方法
1. shouldOverrideUrlLoading(WebView view, String url)

    官方注释: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". 

翻译:当一个新的url要在当前WebView进行加载的时候,这个方法给应用一个机会来控制url的处理。如果WebView没有setWebViewClient,则默认操作是WebView将询问Activity Manager获取合适的handler处理url。如果WebView设置了setWebViewClient,返回true代表当前应用来处理url,返回false则代表当前webview来处理url。如果http请求是POST方法,该方法将不会被调用。
代码示例:

 

  /** 
   * 所有以www.example.com开头的url调用系统浏览器打开 其他的url在当前webview打开 
   */ 
  @Override 
  public boolean shouldOverrideUrlLoading(WebView view, String url) { 
    if (url.indexOf("http://www.example.com") != -1) { 
      // 调用系统默认浏览器处理url 
      view.stopLoading(); 
      view.getContext().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url))); 
      return true; 
    } 
    return false; 
  } 

2. shouleOverrideKeyEvent(WebView view, KeyEvent event)

    官方注释: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. 

翻译:给当前应用一个机会来异步处理按键事件。返回true,WebView将不会处理该按键事件,返回false,WebView将处理该按键事件。默认返回是false。
3. onPageStarted(WebView view, String url, Bitmap favicon)和onPageFinished(WebView view, String url)

    官方注释: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. clicking a link whose target is an iframe. 

翻译:当页面开始加载时被调用。但是,当页面被嵌套时(例如iframe里有一个链接跳转),该方法将不会被调用。(今天就遇到了这种情况,可以通过重载onLoadResource来控制url跳转)

    官方注释: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). 

翻译:在页面加载结束时被调用。
代码示例:

    // 获取页面加载时间  
   

 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. 

翻译:通知应用程序WebView将要加载指定url的资源,每一个资源(例如图片,嵌套url,js,css文件)。(可以通过该方法处理iframe嵌套的url)
代码示例:

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

相关文章

  • XML Web 服务 Eclipse实现sun-jaxws.xml文件的方法

    XML Web 服务 Eclipse实现sun-jaxws.xml文件的方法

    在sun-jaxws.xml文件,可以配置endpoint、handler-chain等内容,在这个文件中配置的内容会覆盖在Java代码中使用注解属性配置的的内容,本文给大家介绍的非常详细,感兴趣的朋友一起看看吧
    2023-11-11
  • Java异常处理实例教程

    Java异常处理实例教程

    这篇文章主要为大家分享一份非常详细的Java异常处理实例教程,帮助大家更好的学习java异常处理,感兴趣的小伙伴们可以参考一下
    2016-02-02
  • Java synchronized偏向锁的核心原理详解

    Java synchronized偏向锁的核心原理详解

    这篇文章主要为大家详细介绍了Java synchronized偏向锁的核心原理,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助
    2022-03-03
  • SpringBoot @ExceptionHandler与@ControllerAdvice异常处理详解

    SpringBoot @ExceptionHandler与@ControllerAdvice异常处理详解

    在Spring Boot应用的开发中,不管是对底层数据库操作,对业务层操作,还是对控制层操作,都会不可避免的遇到各种可预知的,不可预知的异常需要处理,如果每个处理过程都单独处理异常,那么系统的代码耦合度会很高,工作量大且不好统一,以后维护的工作量也很大
    2022-10-10
  • Java实战项目 健身管理系统

    Java实战项目 健身管理系统

    本文是一个Java语言编写的实战项目,是一个健身管理系统,主要用到了ssm+springboot等技术,技术含量笔记高,感兴趣的童鞋跟着小编往下看吧
    2021-09-09
  • spring-security关于hasRole的坑及解决

    spring-security关于hasRole的坑及解决

    这篇文章主要介绍了spring-security关于hasRole的坑及解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-09-09
  • Java中Properties的使用详解

    Java中Properties的使用详解

    这篇文章主要介绍了Java中Properties的使用详解的相关资料,需要的朋友可以参考下
    2016-05-05
  • 详细聊聊Spring MVC重定向与转发

    详细聊聊Spring MVC重定向与转发

    大家应该都知道请求重定向和请求转发都是web开发中资源跳转的方式,这篇文章主要给大家介绍了关于Spring MVC重定向与转发的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考下
    2021-09-09
  • 避免多个jar通过maven打包导致同名配置文件覆盖冲突问题

    避免多个jar通过maven打包导致同名配置文件覆盖冲突问题

    这篇文章主要介绍了避免多个jar通过maven打包导致同名配置文件覆盖冲突问题,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-05-05
  • Java代码中与Lua相互调用实现详解

    Java代码中与Lua相互调用实现详解

    这篇文章主要为大家介绍了Java代码中与Lua相互调用实现详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-08-08

最新评论