Android WebView WebChromeClient : inaccurate progress value in onProgressChanged()

自古美人都是妖i 提交于 2019-12-04 08:34:51
Juan Acevedo

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

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

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!

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