使用WebView客户端计算给定URL的WebView下载数据? [英] Calculating data downloaded by webview for a given url by using webview client?

查看:80
本文介绍了使用WebView客户端计算给定URL的WebView下载数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试计算在 WebView 中下载的数据.以下是我的 WebViewClient

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

以下是日志:

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 上呈现数据.请对此有所启发.预先感谢.

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.

推荐答案

可能正在发生大量的事情.例如,WebView可能正在从缓存中获取资源,进度可能反映了数据负载:URL等.

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..

onProgressChanged API仅用于驱动进度条,因此将从其获取的值应视为最佳猜测".

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'.

这篇关于使用WebView客户端计算给定URL的WebView下载数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆