android - 使用webview客户端计算给定url的webview下载的数据?

标签 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() 最终被调用,所以肯定是这样time 不用于在 WebView 上渲染数据。请赐教我这一点。提前致谢。

最佳答案

可能会发生很多事情。例如,WebView 可能会从缓存获取资源,进度可能会反射(reflect)数据负载:URL 等。

onProgressChanged API 仅用于驱动进度条,因此您从中获得的值应被视为“最佳猜测”。

关于android - 使用webview客户端计算给定url的webview下载的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61473328/

相关文章:

android - Offscreenpagelimit vs setItemViewCacheSize ViewPager2

安卓 - java.lang.SecurityException : Connection refused

Android WebView 不显示站点

Android WebClient,通过WebResourceResponse 返回图像资源 - 不显示图像

java - 网页 View : add custom headers when user click webview links

java - 使用 SQLite 创建表时出错

android - 如何使用图层列表在android中制作带有顶部和底部阴影的矩形?

Android 网页 View : HTML Content is Overlapping

javascript - 如何使用innerHtml正确插入html?

Android webview 获取 sslError SSL_UNTRUSTED 但证书有效