johns4ta
johns4ta

Reputation: 896

Calculating time it takes for a webpage to fully load in a webview

I have an application that reads websites from a text file and then calculates the time it takes to fully load the webpage into the webview. I try to calculate the time by getting the time in milliseconds before I load the url into the webview and then get the time after the webpage has finished loading in the webview and taking the difference between the two, but it always comes out to 0. Any ideas why? The time it takes to load a webpage should be greater than 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");
}

Upvotes: 1

Views: 2338

Answers (4)

onusopus
onusopus

Reputation: 1244

One possible solution would be a combination of onPageStarted, onPageFinished and onProgressChanged.

Start the timer in onPageStarted End the timer in 100% of onProgressChanged or onPageFinished which ever is later.

Upvotes: 0

Victor Laerte
Victor Laerte

Reputation: 6556

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
            }

Upvotes: 3

Parag Chauhan
Parag Chauhan

Reputation: 35946

Using on onProgressChanged method you can do this.

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

                     });

Upvotes: 2

David Freitag
David Freitag

Reputation: 2272

It looks like you aren't setting startTime until after you have calculated the difference in time from the initial page load. You should put

startTime = cal.getTimeInMillis();

before the

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

line. Otherwise when the initial page gets loaded, you are effectively subtracting 0 from endTime.

Upvotes: 0

Related Questions