使用webview客户端计算webview为给定网址下载的数据?

时间:2014-05-06 09:24:26

标签: android android-webview webviewclient

我正在尝试计算在WebView中下载的数据。以下是我的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()");
        }
    };

还要计算我正在使用的数据:

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);
            }
        });

以下是日志:

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

正如我们可以看到数据字节在一段时间后变得不变,我想知道在那段时间内发生了什么?最后调用了onPageFinished()所以这次肯定不会用于在WebView上呈现数据。请在此赐教我。提前谢谢。

1 个答案:

答案 0 :(得分:2)

可能会发生很多事情。例如,WebView可能从缓存获取资源,进度可能反映了大量数据:URL等。

onProgressChanged API仅用于推动进度条,因此您从中获取的值应被视为最佳猜测'。