18216

Can you measure the time it takes to load a web page in a webView in Android?

There are lots of questions regarding the WebView in Android taking too long to load, however I'd like to be able to time exactly how long it takes to completely load, and then display the result.

Is this possible? I could start a timer but does the WebView have an HTML OnLoad style call once it has loaded?

The code I have is basic:

/** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); setContentView(R.layout.pageloadtest); webView = (WebView) findViewById(R.id.webView1); webView.loadUrl("http://www.airometricwireless.com"); }

This loads the page fine. I just need to time how long it takes.

Answer1:

Probably not scientifically accurately, but I guess you could call

long a, b; mWebView.setWebViewClient(new WebViewClient() { public void onPageStarted(WebView view, String url) { a = (new Date()).getTime; } public void onPageFinished(WebView view, String url) { b = (new Date()).getTime; } });

Then b - a.

Hope it helps.

Best regards.

Answer2:

To get page loading finish, use

webView.setWebViewClient(new WebViewClient() { public void onPageFinished(WebView view, String url) { // do your stuff here } });

Answer3:

You can use onPageFinished but as the docs say, "When onPageFinished() is called, the rendering picture may not be updated yet."

Also it's only meaningful for simple pages as it won't factor in IFRAMEs or dynamically-loaded content e.g. JavaScript that loads other JavaScript such as social media buttons, adverts, tracking scripts etc. And it's typically that stuff that causes pages to feel unperformant.

What people often mean when they say "it's slow to load" is "it takes a long time before I can interact with the page" which is very different from how long it takes to load the assets. For this you probably want to test when an onDOMReady-like event fires from JavaScript, or if not using that, when non-deferred JS actually initialises.

Essentially, as soon as you have any dynamic loading behaviours in the page, onPageFinished becomes meaningless, and you need to measure with JS.

As with any performance problem;

<ol> <li>measure the interaction to discover the bottlenecks</li> <li>try to fix a bottleneck & measure again</li> <li>repeat until performance is acceptable.</li> </ol>

Recommend

  • Error on dismissing ProgressDialog in AsyncTask
  • Android muliple WebView save instance
  • How to call your own ASP.NET MVC project preaent on same machine from xamrin webview
  • Android - How to have Mixed Orientation Tabs? (some tabs portrait, others landscape)
  • WebView: webpage not available but I load it from an html string
  • Phonegap Android App, open links within app?
  • input type=“file” accept=“image/*” doesn't work in phone gap?
  • Eventbus onMessageEvent not getting called
  • Why is OpenID Connect considered mobile friendly compared to SAML
  • How to Handle Click on Imageview in Custom Listview with image and Textview?
  • Android: How do you create an EditText field in java class
  • setContentView() is not enough to switch between layouts?
  • CloseOptionsMenu doesn't work?
  • Unable to resolve Static method?
  • does jqgrid support a multiple checkbox list for editing
  • Optimizing database types to compact database (SQLite)
  • Cross-Platform Protobuf Serialization
  • jquery mobile loadPage not working
  • Do I've to free mysql result after storing it?
  • Python - Map / Reduce - How do I read JSON specific field in using DISCO count words example
  • Warning: Can't call setState (or forceUpdate) on an unmounted component
  • bootstrap to use multiple ng-app
  • How to get icons for entities from eclipse?
  • How to delete a row from a dynamic generate table using jquery?
  • json Serialization in asp
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • How to stop GridView from loading again when I press back button?
  • Turn off referential integrity in Derby? is it possible?
  • JaxB to read class hierarchy