I need to access the progress value in WebView -> WebChromeClient -> onProgressChanged(). The progress integer value doesn't increment from 0 to 100 but rather jumps around. Here is a sample log-output from loading one page and the associated progress numbers:
DEBUG: progress : 10
DEBUG: progress : 100
DEBUG: progress : 10
DEBUG: progress : 100
DEBUG: progress : 10
DEBUG: progress : 100
DEBUG: progress : 10
DEBUG: progress : 10
DEBUG: progress : 100
DEBUG: progress : 10
DEBUG: progress : 100
DEBUG: progress : 10
DEBUG: progress : 100
DEBUG: progress : 10
DEBUG: progress : 100
DEBUG: progress : 10
DEBUG: progress : 30
DEBUG: progress : 100
DEBUG: progress : 10
DEBUG: progress : 30
DEBUG: progress : 100
DEBUG: progress : 10
DEBUG: progress : 30
DEBUG: progress : 37
DEBUG: progress : 45
DEBUG: progress : 48
DEBUG: progress : 49
DEBUG: progress : 50
DEBUG: progress : 52
DEBUG: progress : 54
DEBUG: progress : 56
DEBUG: progress : 59
DEBUG: progress : 61
DEBUG: progress : 63
DEBUG: progress : 66
DEBUG: progress : 68
DEBUG: progress : 70
DEBUG: progress : 73
DEBUG: progress : 75
DEBUG: progress : 77
DEBUG: progress : 79
DEBUG: progress : 82
DEBUG: progress : 84
DEBUG: progress : 86
DEBUG: progress : 87
DEBUG: progress : 88
DEBUG: progress : 89
DEBUG: progress : 100
What am I doing wrong?
Code:
webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
Log.i(LOG_TAG, "DEBUG: progress : " + progress);
}
});
From my own experiments, I found that onProgressChanged can get called multiple times for the same url for the same progress value (Ex: google.com progress = 100 calls twice).
I also found that onProgressChanged will be called late for a url while you are loading another one. Timetable example:
At this point, I should mention that in my experiments I override shouldOverrideUrlLoading() to see redirects and even when I don't see redirects the above still happens. So why does this happen?
According to this answer( Why is there a noticeable delay between WebChromeClient.onProgressChanged and jquery's $(document).ready() callbacks?): because a page is loaded before the script are triggered, document ready looks for all html elements to be loaded first except images and things like that, so your script does not fire until everything has been loaded, by that time your onprogresschanged has received 100% first as it may be have a higher priority than your javascript after all devices do ask to check if js should be allowed or not so it has to go through some checks before this method is called.
So to put it all together this is what you need to do:
Code
private boolean isCurrentUrl(String url){
return url.toLowerCase().contains(currentUrl.toLowerCase());
}
public void onPageStarted(WebView webView, String url,
android.graphics.Bitmap bitmap){
if (isCurrentUrl(url)){
pageLoaded = false;
}
}
public void onPageFinished(WebView webview, String url){
if (isCurrentUrl(url)){
pageLoaded = true;
}
}
public boolean shouldOverrideUrlLoading(WebView view, String
url){
if (!currentUrl.equalsIgnoreCase(url)){
currentUrl = url;
view.loadUrl(currentUrl);
return true;
}
return false;
}
As @ksasq pointed out in the above comment, the initial URL caused many redirects and thus onProgressChanged() is called many times.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With