簡談WebViewClient與WebChromeClient

來源:互聯網
上載者:User

標籤:

簡介:WebViewClient被用來傳遞單純的載入一個連結時所發生的事件,比如開始載入,結束載入等,它代表這個連結載入時的最普通的和最籠統的事件,WebChromeClient更多的是傳遞JS對話方塊,上傳檔案,網頁的標題改變等網頁內元素的事件。

 

 

 一,WebViewClient

  1. API列表:
 
?void doUpdateVisitedHistory(WebView view, String url, boolean isReload)

知主應用程式來更新其訪問過的連結資料庫。

void onFormResubmission(WebView view, Message dontResend, Message resend)

作為主機的應用程式,如果在瀏覽器應該重新發送的資料作為所請求的頁是一個POST的結果。

void onLoadResource(WebView view, String url)

通知主應用程式的web視圖將載入由給定的URL指定的資源。

void onPageFinished(WebView view, String url)

通知主應用程式頁面載入完成。

void onPageStarted(WebView view, String url, Bitmap favicon)

通知主應用程式一個頁面已經開始載入。

void onReceivedClientCertRequest(WebView view, ClientCertRequest request)

通知主應用程式來處理SSL用戶端認證請求。

void onReceivedError(WebView view, int errorCode, String description, String failingUrl)

報告錯誤到主應用程式。

void onReceivedHttpAuthRequest(WebView view, HttpAuthHandler handler, String host, String realm)

通知主應用程式WebView接收到一個HTTP驗證請求

void onReceivedLoginRequest(WebView view, String realm, String account, String args)

通知主應用程式一個登陸請求已經被處理

void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error)

通知主應用程式在載入資源是發生了一個SSL 錯誤

void onScaleChanged(WebView view, float oldScale, float newScale)

通知主應用程式WebView的縮放狀態已經被改變

void onTooManyRedirects(WebView view, Message cancelMsg, Message continueMsg)

這個方法在API8的時候被遺棄。這個方法已經不再被調用,當WebView遇到一個重新導向的迴圈操作,它會取消這個操作

void onUnhandledInputEvent(WebView view, InputEvent event)

通知主應用程式一個鍵盤輸入事件沒有被WebView傳遞

void onUnhandledKeyEvent(WebView view, KeyEvent event)

這個方法在API 21的時候被遺棄,它被歸入onUnhandledKeyEvent。

WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request)

通知主應用程式有個一個資源請求然後允許應用程式返回資料

WebResourceResponse shouldInterceptRequest(WebView view, String url)

這個方法在API 21 的時候被遺棄,使用shouldInterceptRequest代替

boolean shouldOverrideKeyEvent(WebView view, KeyEvent event)

給主應用程式提供一個機會去傳遞鍵盤同步事件

boolean shouldOverrideUrlLoading(WebView view, String url)

給主應用程式提供一個機會去控制當前WebView將要被載入的新的url

     

  2. 下面介紹下幾個簡單的應用:

         

  •        使用我們自己的WebView載入頁面上的連結

               當使用者點擊一個網頁上的串連時,預設的操作是另外開啟一個與之相關的應用然後傳遞URL,通常是開啟我們系統預設的瀏覽器然後載入目標URL地址。                  但是我們可以重新定義這個操作既使用我們自己的WebView開啟開啟這個URL,你還可以通過他們的瀏覽曆史允許使用者導航向前像後。能夠到達上面的效                果西需要我們提供一個自己的WebView,然後適用setWebViewClient()

 

WebView webview = f(WebView)indViewById(R.id.webview);webview.setWebViewClient(new WebViewClient());

 

               這個運行使用者使用你自己的WebView載入連結

               如果想要在點擊一個串連後擷取到更多的控制,需要我們建立我們自己的WebClient,然後重寫shouldOverrideUrlLoading() 方法,

               例如:我們如果點擊的我連結是我自己的就用我們自己的WebView載入,如果是別的連結就用別的應用開啟

 

private class MyWebViewClient extends WebViewClient {    @Override    public boolean shouldOverrideUrlLoading(WebView view, String url) {               if (Uri.parse(url).getHost().equals("www.example.com")) {            // This is my web site, so do not override; let my WebView                load  the page            return false;        }        // Otherwise, the link is not for a page on my site, so launch             another Activity that handles URLs        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));        startActivity(intent);        return true;    }}

然後:

WebView myWebView = (WebView) findViewById(R.id.webview);myWebView.setWebViewClient(new MyWebViewClient());

showOverrideUrlLoading的傳回值為false時代表我們自己的WebView會處理這個連結,如果為true代表會傳遞給系統做相應的處理(如:提供一些能夠處理這個連線應用程式讓你選擇)

 

  • 導航網頁瀏覽曆史

        當我們重寫URL載入時,它會自動的累計你的網頁瀏覽曆史,你可以通過goBack()和goForward()向前或者向後導航。

        例如:我們的Activity使用返回鍵處理返回導航

@Overridepublic boolean onKeyDown(int keyCode, KeyEvent event) {    // Check if the key event was the Back button and if there‘s history    if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {        myWebView.goBack();        return true;    }    // If it wasn‘t the Back key or there‘s no web page history, bubble up to the default    // system behavior (probably exit the activity)    return super.onKeyDown(keyCode, event);}

如果不進行這個檢查,當使用者已經瀏覽器完所有的曆史之後goBack()和goForward()將不起任何作用。

 

簡談WebViewClient與WebChromeClient

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.