最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

在Android系統(tǒng)中使用WebViewClient處理跳轉(zhuǎn)URL的方法

 更新時(shí)間:2015年07月31日 14:52:30   作者:低調(diào)小一  
這篇文章主要介紹了在Android系統(tǒng)中使用WebViewClient處理跳轉(zhuǎn)URL的方法,實(shí)現(xiàn)代碼為Java語(yǔ)言編寫,是需要的朋友可以參考下

前言
最近代碼里和WebView有很多的交互,webview是android中的瀏覽器控件,這里主要介紹一下webview如何重載WebViewClient類來(lái)控制URL加載。

使用WebViewClient
使用WebViewClinet主要是繼承WebViewClient父類,根據(jù)需要重寫其中的方法,并在WebView中進(jìn)行配置,示例代碼如下:

   

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

翻譯:當(dāng)一個(gè)新的url要在當(dāng)前WebView進(jìn)行加載的時(shí)候,這個(gè)方法給應(yīng)用一個(gè)機(jī)會(huì)來(lái)控制url的處理。如果WebView沒(méi)有setWebViewClient,則默認(rèn)操作是WebView將詢問(wèn)Activity Manager獲取合適的handler處理url。如果WebView設(shè)置了setWebViewClient,返回true代表當(dāng)前應(yīng)用來(lái)處理url,返回false則代表當(dāng)前webview來(lái)處理url。如果http請(qǐng)求是POST方法,該方法將不會(huì)被調(diào)用。
代碼示例:

 

  /** 
   * 所有以www.example.com開頭的url調(diào)用系統(tǒng)瀏覽器打開 其他的url在當(dāng)前webview打開 
   */ 
  @Override 
  public boolean shouldOverrideUrlLoading(WebView view, String url) { 
    if (url.indexOf("http://www.example.com") != -1) { 
      // 調(diào)用系統(tǒng)默認(rèn)瀏覽器處理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. 

翻譯:給當(dāng)前應(yīng)用一個(gè)機(jī)會(huì)來(lái)異步處理按鍵事件。返回true,WebView將不會(huì)處理該按鍵事件,返回false,WebView將處理該按鍵事件。默認(rèn)返回是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. 

翻譯:當(dāng)頁(yè)面開始加載時(shí)被調(diào)用。但是,當(dāng)頁(yè)面被嵌套時(shí)(例如iframe里有一個(gè)鏈接跳轉(zhuǎn)),該方法將不會(huì)被調(diào)用。(今天就遇到了這種情況,可以通過(guò)重載onLoadResource來(lái)控制url跳轉(zhuǎn))

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

翻譯:在頁(yè)面加載結(jié)束時(shí)被調(diào)用。
代碼示例:

    // 獲取頁(yè)面加載時(shí)間  
   

 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. 

翻譯:通知應(yīng)用程序WebView將要加載指定url的資源,每一個(gè)資源(例如圖片,嵌套u(yù)rl,js,css文件)。(可以通過(guò)該方法處理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))); 
    }       
  } 

相關(guān)文章

  • XML Web 服務(wù) Eclipse實(shí)現(xiàn)sun-jaxws.xml文件的方法

    XML Web 服務(wù) Eclipse實(shí)現(xiàn)sun-jaxws.xml文件的方法

    在sun-jaxws.xml文件,可以配置endpoint、handler-chain等內(nèi)容,在這個(gè)文件中配置的內(nèi)容會(huì)覆蓋在Java代碼中使用注解屬性配置的的內(nèi)容,本文給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧
    2023-11-11
  • Java異常處理實(shí)例教程

    Java異常處理實(shí)例教程

    這篇文章主要為大家分享一份非常詳細(xì)的Java異常處理實(shí)例教程,幫助大家更好的學(xué)習(xí)java異常處理,感興趣的小伙伴們可以參考一下
    2016-02-02
  • Java synchronized偏向鎖的核心原理詳解

    Java synchronized偏向鎖的核心原理詳解

    這篇文章主要為大家詳細(xì)介紹了Java synchronized偏向鎖的核心原理,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助
    2022-03-03
  • SpringBoot @ExceptionHandler與@ControllerAdvice異常處理詳解

    SpringBoot @ExceptionHandler與@ControllerAdvice異常處理詳解

    在Spring Boot應(yīng)用的開發(fā)中,不管是對(duì)底層數(shù)據(jù)庫(kù)操作,對(duì)業(yè)務(wù)層操作,還是對(duì)控制層操作,都會(huì)不可避免的遇到各種可預(yù)知的,不可預(yù)知的異常需要處理,如果每個(gè)處理過(guò)程都單獨(dú)處理異常,那么系統(tǒng)的代碼耦合度會(huì)很高,工作量大且不好統(tǒng)一,以后維護(hù)的工作量也很大
    2022-10-10
  • Java實(shí)戰(zhàn)項(xiàng)目 健身管理系統(tǒng)

    Java實(shí)戰(zhàn)項(xiàng)目 健身管理系統(tǒng)

    本文是一個(gè)Java語(yǔ)言編寫的實(shí)戰(zhàn)項(xiàng)目,是一個(gè)健身管理系統(tǒng),主要用到了ssm+springboot等技術(shù),技術(shù)含量筆記高,感興趣的童鞋跟著小編往下看吧
    2021-09-09
  • spring-security關(guān)于hasRole的坑及解決

    spring-security關(guān)于hasRole的坑及解決

    這篇文章主要介紹了spring-security關(guān)于hasRole的坑及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • Java中Properties的使用詳解

    Java中Properties的使用詳解

    這篇文章主要介紹了Java中Properties的使用詳解的相關(guān)資料,需要的朋友可以參考下
    2016-05-05
  • 詳細(xì)聊聊Spring MVC重定向與轉(zhuǎn)發(fā)

    詳細(xì)聊聊Spring MVC重定向與轉(zhuǎn)發(fā)

    大家應(yīng)該都知道請(qǐng)求重定向和請(qǐng)求轉(zhuǎn)發(fā)都是web開發(fā)中資源跳轉(zhuǎn)的方式,這篇文章主要給大家介紹了關(guān)于Spring MVC重定向與轉(zhuǎn)發(fā)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2021-09-09
  • 避免多個(gè)jar通過(guò)maven打包導(dǎo)致同名配置文件覆蓋沖突問(wèn)題

    避免多個(gè)jar通過(guò)maven打包導(dǎo)致同名配置文件覆蓋沖突問(wèn)題

    這篇文章主要介紹了避免多個(gè)jar通過(guò)maven打包導(dǎo)致同名配置文件覆蓋沖突問(wèn)題,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-05-05
  • Java代碼中與Lua相互調(diào)用實(shí)現(xiàn)詳解

    Java代碼中與Lua相互調(diào)用實(shí)現(xiàn)詳解

    這篇文章主要為大家介紹了Java代碼中與Lua相互調(diào)用實(shí)現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-08-08

最新評(píng)論

长宁县| 广德县| 新泰市| 壤塘县| 蓝田县| 宿松县| 崇阳县| 顺平县| 冕宁县| 吉隆县| 宣威市| 岑巩县| 武冈市| 乐山市| 唐河县| 饶平县| 紫阳县| 筠连县| 乐安县| 佛教| 垦利县| 都兰县| 神池县| 涿州市| 邢台县| 固原市| 浦城县| 图木舒克市| 文登市| 清远市| 新巴尔虎右旗| 普兰店市| 大邑县| 临潭县| 上饶市| 许昌县| 凤山县| 双城市| 永和县| 迁西县| 张掖市|