计算网页在网页浏览中完全加载所需的时间

时间:2013-06-27 19:36:42

标签: java android

我有一个应用程序从文本文件中读取网站,然后计算将网页完全加载到webview所需的时间。我尝试通过在将URL加载到webview之前获取时间(以毫秒为单位)来计算时间,然后在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 个答案:

答案 0 :(得分:4)

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
            }

答案 1 :(得分:2)

使用onProgressChanged方法,您可以执行此操作。

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

                     });

答案 2 :(得分:0)

在计算出与初始页面加载时间的差异之前,您似乎没有设置startTime。你应该把

startTime = cal.getTimeInMillis();

之前的

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

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

答案 3 :(得分:0)

一种可能的解决方案是onPageStarted,onPageFinished和onProgressChanged的组合。

在onPageStarted中启动计时器 以100%的onProgressChanged或onPageFinished结束计时器。