Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android中監聽Phonegap的webview的start,finish等事件

Android中監聽Phonegap的webview的start,finish等事件

編輯:關於Android編程

之前寫過一篇捕獲Phoengap的webview事件的方法,主要是在實現了CordovaInterface的Activity中,  在onMessage中根據第一個參數的message name來判斷webview的加載事件。不過如果是在Fragment中,一個Activity加載了多個包含了WebView的fragment的話,就無法在onMessage中判斷到底是哪個WebView的加載事件。

經過嘗試,發現,還是可以使用setWebViewClient的方式來,來重寫WebViewClient的對應的onPageStart和onPageFinish等方法來處理加載事件


[java]
CordovaWebView webView = (CordovaWebView)findViewById(R.id.webView); 

 CordovaWebView webView = (CordovaWebView)findViewById(R.id.webView);[java] view plaincopyprint?CordovaInterface cordovaInterface = (CordovaInterface) context; 
 
       CordovaWebViewClient cordovaWebViewClient = new CordovaWebViewClient(cordovaInterface, webView) { 
           @Override 
           public void onPageStarted(WebView view, String url, Bitmap favicon) { 
 
               LogUtil.debug("onPageStarted " + url); 
               super.onPageStarted(view, url, favicon); 
               progressBar.setVisibility(View.VISIBLE); 
           } 
 
           @Override 
           public void onPageFinished(WebView view, String url) { 
               LogUtil.debug("onPageFinished " + url); 
               super.onPageFinished(view, url); 
               progressBar.setVisibility(View.GONE); 
           } 
 
           @Override 
           public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { 
               LogUtil.debug("onReceivedError " + url); 
               super.onReceivedError(view, errorCode, description, failingUrl); 
               progressBar.setVisibility(View.GONE); 
               errorView.setVisibility(View.VISIBLE); 
           } 
       }; 
       webView.setWebViewClient(cordovaWebViewClient); 
 
       webView.loadUrl(url); 

 CordovaInterface cordovaInterface = (CordovaInterface) context;

        CordovaWebViewClient cordovaWebViewClient = new CordovaWebViewClient(cordovaInterface, webView) {
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {

                LogUtil.debug("onPageStarted " + url);
                super.onPageStarted(view, url, favicon);
                progressBar.setVisibility(View.VISIBLE);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                LogUtil.debug("onPageFinished " + url);
                super.onPageFinished(view, url);
                progressBar.setVisibility(View.GONE);
            }

            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                LogUtil.debug("onReceivedError " + url);
                super.onReceivedError(view, errorCode, description, failingUrl);
                progressBar.setVisibility(View.GONE);
                errorView.setVisibility(View.VISIBLE);
            }
        };
        webView.setWebViewClient(cordovaWebViewClient);

        webView.loadUrl(url);

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved