Search code examples
androidandroid-webviewwebviewclient

Calculating data downloaded by webview for a given url by using webview client?


I'm trying to calculate the data downloaded in my WebView. Following is my WebViewClient

WebViewClient mWebViewClient = new WebViewClient() {

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            Log.e(TAG, "init onPageFinished()");
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            Log.e(TAG, "init onPageStarted()");
        }
    };

Also to calculate data I'm using:

mWebView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                long currentBytes = TrafficStats.getUidRxBytes(Process.myUid());
                long totalBytes = currentBytes - previousBytes;
                Log.e(TAG, "Current Bytes ==>   " + totalBytes
                        + "   New Progress ==>   " + newProgress);
            }
        });

Following are th Logs:

05-06 13:11:11.621: E/TAG(16381): init onPageStarted()
05-06 13:11:11.621: E/TAG(16381): Current Bytes ==>   0   New Progress ==>   10
05-06 13:11:12.422: E/TAG(16381): Current Bytes ==>   2736   New Progress ==>   11
05-06 13:11:12.712: E/TAG(16381): Current Bytes ==>   5743   New Progress ==>   12
05-06 13:11:13.353: E/TAG(16381): Current Bytes ==>   28084   New Progress ==>   15
05-06 13:11:13.393: E/TAG(16381): Current Bytes ==>   42576   New Progress ==>   17
05-06 13:11:14.674: E/TAG(16381): Current Bytes ==>   414446   New Progress ==>   18
05-06 13:11:15.125: E/TAG(16381): Current Bytes ==>   527120   New Progress ==>   19
05-06 13:11:15.125: E/TAG(16381): Current Bytes ==>   629186   New Progress ==>   22
05-06 13:11:15.125: E/TAG(16381): Current Bytes ==>   629186   New Progress ==>   24
05-06 13:11:15.145: E/TAG(16381): Current Bytes ==>   629186   New Progress ==>   26
05-06 13:11:15.155: E/TAG(16381): Current Bytes ==>   629186   New Progress ==>   29
05-06 13:11:15.155: E/TAG(16381): Current Bytes ==>   629186   New Progress ==>   31
05-06 13:11:15.175: E/TAG(16381): Current Bytes ==>   629186   New Progress ==>   34
05-06 13:11:16.576: E/TAG(16381): Current Bytes ==>   629186   New Progress ==>   46
05-06 13:11:17.107: E/TAG(16381): Current Bytes ==>   629186   New Progress ==>   51
05-06 13:11:17.127: E/TAG(16381): Current Bytes ==>   629186   New Progress ==>   55
05-06 13:11:17.157: E/TAG(16381): Current Bytes ==>   629186   New Progress ==>   57
05-06 13:11:17.167: E/TAG(16381): Current Bytes ==>   629186   New Progress ==>   59
05-06 13:11:17.177: E/TAG(16381): Current Bytes ==>   629186   New Progress ==>   72
05-06 13:11:17.187: E/TAG(16381): Current Bytes ==>   629186   New Progress ==>   79
05-06 13:11:17.197: E/TAG(16381): Current Bytes ==>   629186   New Progress ==>   81
05-06 13:11:17.197: E/TAG(16381): Current Bytes ==>   629186   New Progress ==>   84
05-06 13:11:17.217: E/TAG(16381): Current Bytes ==>   629186   New Progress ==>   88
05-06 13:11:17.517: E/TAG(16381): init onPageFinished()
05-06 13:11:17.517: E/TAG(16381): Current Bytes ==>   629186   New Progress ==>   100

As we can see that the data bytes gets constant after a certain period of time,I wanna know that what is happening in that time span?onPageFinished() is called in the end so definitely this time is not used for rendering the data on the WebView. Kindly enlighten me on this. Thanks in advance.


Solution

  • Tons of stuff might be happening. The WebView might be getting resources from cache for example, the progress could be reflecting loads of data: URLs, etc..

    The onProgressChanged API was only intended to drive a progress bar, so the values you get from it should be treated as a 'best guess'.