1

我有一个应用程序从文本文件中读取网站,然后计算将网页完全加载到 webview 中所需的时间。我尝试通过在将 url 加载到 webview 之前获取时间(以毫秒为单位)来计算时间,然后在网页完成加载 webview 后获取时间并获取两者之间的差异,但它始终为 0。任何想法为什么?加载一个网页的时间应该大于0?

public class MainActivity extends Activity {
    // NUMSITES will be the number of websites contained in the sites.txt file.
    int NUMSITES = 4;
    int curSite = 0;
    long startTime = 0;
    long endTime = 0;
    long totTime = 0;
    boolean start = true;
    Calendar cal = Calendar.getInstance();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_main);
    final WebView wb = (WebView) findViewById(R.id.webView1);
    final String[] sites = new String[NUMSITES];
    getWebsites(sites);

    // Listed as optimal settings for HTML5 (may need testing?).
    // Ref.
    // http://stackoverflow.com/questions/10097233/optimal-webview-settings-for-html5-support
    wb.getSettings().setJavaScriptEnabled(true);
    WebSettings settings = wb.getSettings();
    settings.setJavaScriptEnabled(true);
    settings.setDomStorageEnabled(true);
    wb.setFocusable(true);
    wb.setFocusableInTouchMode(true);
    wb.getSettings().setRenderPriority(RenderPriority.HIGH);
    wb.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
    wb.getSettings().setDatabaseEnabled(true);
    wb.getSettings().setAppCacheEnabled(true);
    wb.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
    WebViewClient client = new WebViewClient() {

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            return true;
        }

        @Override
        public void onLoadResource(WebView view, String url) {
        }

        @Override
        public WebResourceResponse shouldInterceptRequest(WebView view,
                String url) {
            return null;
        }

        // On page finished, retrive next website from string array and load
        // it.
        @Override
        public void onPageFinished(WebView view, String url) {
            //
            endTime = cal.getTimeInMillis();
            totTime = endTime - startTime;
            Log.d("Time", "Time: " + totTime);
            Log.d("Time", "Start Time: " + startTime);
            Log.d("Time", "End Time: " + endTime);
            if (curSite < NUMSITES) {
                startTime = cal.getTimeInMillis();
                wb.loadUrl(sites[curSite]);

                curSite++;
            }
        }

    };

    wb.setWebViewClient(client);
    // Initial webpage
    wb.loadUrl("https://sites.google.com/site/imagesizetesting/one-1");
}
4

4 回答 4

3
private long startTime;
private long finishedTime;

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {

                 startTime = System.nanoTime();

            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);

                finishedTime = System.nanoTime();

                long timeToFullLoad = finishedTime - startTime; // It will be in NANO, just convert it to seconds and enjoy
            }
于 2013-06-27T20:05:45.803 回答
2

使用 ononProgressChanged方法可以做到这一点。

                 Calendar cal = Calendar.getInstance();
                 long startTime = cal.getTimeInMillis();
                 long endTime = 0;
                 wv.setWebChromeClient(new WebChromeClient() {
                        public void onProgressChanged(WebView view, int progress) {
                            if(progress == 100 )
                                endTime = cal.getTimeInMillis();
                        }

                     });
于 2013-06-27T20:01:58.567 回答
0

看起来您startTime直到计算出与初始页面加载的时间差后才进行设置。你应该把

startTime = cal.getTimeInMillis();

之前

wb.loadUrl("https://sites.google.com/site/imagesizetesting/one-1");

线。否则,当初始页面被加载时,您实际上是从 endTime 中减去 0。

于 2013-06-27T19:46:50.367 回答
0

一种可能的解决方案是结合使用 onPageStarted、onPageFinished 和 onProgressChanged。

在 onPageStarted 中启动计时器 在 onProgressChanged 或 onPageFinished 的 100% 中结束计时器,以较晚的时间为准。

于 2016-03-15T15:44:18.340 回答