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

所属分类: 软件编程 / java 阅读数: 44
收藏 0 赞 0 分享

前言
最近代码里和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))); 
    }       
  } 

更多精彩内容其他人还在看

SpringBoot中使用Ehcache的详细教程

EhCache 是一个纯 Java 的进程内缓存框架,具有快速、精干等特点,是 Hibernate 中默认的 CacheProvider。这篇文章主要介绍了SpringBoot中使用Ehcache的相关知识,需要的朋友可以参考下
收藏 0 赞 0 分享

在idea 中添加和删除模块Module操作

这篇文章主要介绍了在idea 中添加和删除模块Module操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

java spring整合junit操作(有详细的分析过程)

这篇文章主要介绍了java spring整合junit操作(有详细的分析过程),具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详解JAVA 弱引用

这篇文章主要介绍了 JAVA 弱引用的相关资料,帮助大家更好的理解和学习java引用对象,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

深入了解JAVA 虚引用

这篇文章主要介绍了JAVA 虚引用的相关资料,帮助大家更好的理解和学习JAVA,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

详解JAVA 强引用

这篇文章主要介绍了JAVA 强引用的相关资料,帮助大家更好的理解和学习,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

java中的按位与(&)用法说明

这篇文章主要介绍了java中的按位与(&)用法说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

深入了解JAVA 软引用

这篇文章主要介绍了JAVA 软引用的相关资料,帮助大家更好的理解和学习,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

利用MyBatis实现条件查询的方法汇总

这篇文章主要给大家介绍了关于利用MyBatis实现条件查询的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者使用MyBatis具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

Intellij IDEA 与maven 版本不符 Unable to import maven project See logs for details: No implementation for org.apache.maven.model.path.PathTranslator was bound

这篇文章主要介绍了Intellij IDEA 与maven 版本不符 Unable to import maven project See logs for details: No implementation for org.apache.maven.model.path.Pa
收藏 0 赞 0 分享
查看更多