Can You Measure The Time It Takes To Load A Web Page In A Webview In Android?
Solution 1:
Probably not scientifically accurately, but I guess you could call
long a, b;
mWebView.setWebViewClient(newWebViewClient() {
publicvoidonPageStarted(WebView view, String url) {
a = (newDate()).getTime;
}
publicvoidonPageFinished(WebView view, String url) {
b = (newDate()).getTime;
}
});
Then b - a.
Hope it helps.
Best regards.
Solution 2:
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;
- measure the interaction to discover the bottlenecks
- try to fix a bottleneck & measure again
- repeat until performance is acceptable.
Solution 3:
To get page loading finish, use
webView.setWebViewClient(newWebViewClient() {
publicvoidonPageFinished(WebView view, String url) {
// do your stuff here
}
});
Post a Comment for "Can You Measure The Time It Takes To Load A Web Page In A Webview In Android?"