亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

在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開(kāi)頭的url調(diào)用系統(tǒng)瀏覽器打開(kāi) 其他的url在當(dāng)前webview打開(kāi) 
   */ 
  @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è)面開(kāi)始加載時(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)文章

  • Java中GUI工具包AWT和Swing用法介紹

    Java中GUI工具包AWT和Swing用法介紹

    這篇文章介紹了Java中GUI工具包AWT和Swing用法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-01-01
  • springmvc用于方法鑒權(quán)的注解攔截器的解決方案代碼

    springmvc用于方法鑒權(quán)的注解攔截器的解決方案代碼

    這篇文章主要介紹了springmvc用于方法鑒權(quán)的注解攔截器的解決方案代碼,具有一定借鑒價(jià)值,需要的朋友可以參考下。
    2017-12-12
  • IntelliJ IDEA報(bào)錯(cuò)Error:java: Compilation failed: internal java compiler error的解決辦法

    IntelliJ IDEA報(bào)錯(cuò)Error:java: Compilation failed: internal java

    今天小編就為大家分享一篇關(guān)于IntelliJ IDEA報(bào)錯(cuò)Error:java: Compilation failed: internal java compiler error的解決辦法,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2018-10-10
  • springboot文件上傳保存路徑的問(wèn)題

    springboot文件上傳保存路徑的問(wèn)題

    這篇文章主要介紹了springboot文件上傳保存路徑的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • java實(shí)現(xiàn)json字符串格式化處理的工具類

    java實(shí)現(xiàn)json字符串格式化處理的工具類

    這篇文章主要為大家詳細(xì)介紹了如何使用java實(shí)現(xiàn)json字符串格式化處理的工具類,文中的示例代碼簡(jiǎn)潔易懂,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2024-01-01
  • SpringBoot過(guò)濾器如何獲取POST請(qǐng)求的JSON參數(shù)

    SpringBoot過(guò)濾器如何獲取POST請(qǐng)求的JSON參數(shù)

    這篇文章主要介紹了SpringBoot過(guò)濾器如何獲取POST請(qǐng)求的JSON參數(shù)操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • 在Intellij IDEA中使用Debug(圖文教程)

    在Intellij IDEA中使用Debug(圖文教程)

    下面小編就為大家?guī)?lái)一篇在Intellij IDEA中使用Debug(圖文教程)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-09-09
  • 10種java數(shù)組合并的常用方法總結(jié)

    10種java數(shù)組合并的常用方法總結(jié)

    在Java中,合并(或連接)數(shù)組是常見(jiàn)的任務(wù),這篇文章主要為大家整理了10個(gè)java中常用的數(shù)組合并方法,文中的示例代碼簡(jiǎn)潔易懂,需要的小伙伴可以參考下
    2023-12-12
  • Java程序結(jié)構(gòu)與常量變量難點(diǎn)解析

    Java程序結(jié)構(gòu)與常量變量難點(diǎn)解析

    JAVA的基本結(jié)構(gòu)就是順序結(jié)構(gòu),除非特別指明,否則就按照順序一句一句執(zhí)行順序結(jié)構(gòu)是最簡(jiǎn)單的算法結(jié)構(gòu),語(yǔ)句與語(yǔ)句之間,框與框之間是按從上到下的順序進(jìn)行的,它是由若干個(gè)依次執(zhí)行的處理步驟組成的,它是任何一個(gè)算法都離不開(kāi)的一種基本算法結(jié)構(gòu)
    2021-10-10
  • mybatis if傳入字符串?dāng)?shù)字踩坑記錄及解決

    mybatis if傳入字符串?dāng)?shù)字踩坑記錄及解決

    這篇文章主要介紹了mybatis if傳入字符串?dāng)?shù)字踩坑記錄及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-02-02

最新評(píng)論