Android WebView WebChromeClient : inaccurate progress value in onProgressChanged()

旧街凉风 提交于 2020-01-01 10:15:28

问题


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

回答1:


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:

  1. Load google.com
  2. ProgressChanged = 100, webView.geturl(): google.com
  3. Load amazon.com
  4. ProgressChanged = 40, webView.getUrl(): amazon.com
  5. ProgressChanged = 100, webView.getUrl(): google.com

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:

  1. Don't trust onProgressChanged().
  2. onPageStarted() check that the current url you are trying to load matches webView.getUrl(). This is your starting point. Set a boolean finishedLoading to false.
  3. Assume your page is loaded when onPageFinished is called and webView.getUrl() matches the url you are trying to load. If no redirects, then set finishedLoading flag to true. However, for redirects, you will need to follow step 3.
  4. override shouldOverrideurlLoading(). Load the new url if it doesn't match the one that was originally loaded (webView.loadUrl(new redirect url)) and set finishedLoading to false.

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



回答2:


As @ksasq pointed out in the above comment, the initial URL caused many redirects and thus onProgressChanged() is called many times.




回答3:


Best solution is to use the progress of webview and not progress(integer value from method onProgressChanged(WebView view, int progress))

psuedo code,

@Override
public void onProgressChanged(WebView view, int progress) {
       progressBar.setProgress(yourWebView.getProgress());
}

This solution works fine in all cases!



来源:https://stackoverflow.com/questions/22212271/android-webview-webchromeclient-inaccurate-progress-value-in-onprogresschanged

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!