本文介绍了Android WebView WebChromeClient:onProgressChanged()中的进度值不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在WebView-> WebChromeClient-> onProgressChanged()中访问进度值.进度整数值不会从0递增到100,而是会跳来跳去.这是加载一页和相关进度编号的示例日志输出:

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

我在做什么错了?

代码:

webView.setWebChromeClient(new WebChromeClient() {
    public void onProgressChanged(WebView view, int progress) {
        Log.i(LOG_TAG, "DEBUG: progress : " + progress);
    }
});

推荐答案

从我自己的实验中,我发现onProgressChanged可以针对相同的URL多​​次调用相同的进度值(例如:google.com progress = 100次调用两次).

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).

我还发现,在加载另一个URL时,将在某个URL的后面调用onProgressChanged.时间表示例:

I also found that onProgressChanged will be called late for a url while you are loading another one. Timetable example:

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

在这一点上,我应该提到的是,在我的实验中,我重写了ShouldOverrideUrlLoading()来查看重定向,即使我没有看到重定向,上述操作仍然会发生.那么为什么会这样呢?

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?

根据此答案(为什么是WebChromeClient.onProgressChanged和jquery的$(document).ready()回调之间有明显的延迟吗?之类的事情,因此您的脚本要等到所有内容加载完毕后才能启动,到那时您的onprogresschanged已先收到100%,因为在所有设备要求检查是否应允许js后,它的优先级可能比javascript高否,因此必须在调用此方法之前进行一些检查.

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. 不信任onProgressChanged().
  2. onPageStarted()检查您要加载的当前URL是否与webView.getUrl()相匹配.这是您的起点.将布尔值finishLoading设置为false.
  3. 假设在调用onPageFinished且webView.getUrl()与您尝试加载的URL匹配时加载了页面.如果没有重定向,则将finishLoading标志设置为true.但是,对于重定向,您将需要执行步骤3.
  4. override shouldOverrideurlLoading().如果新的URL与最初加载的URL不匹配(webView.loadUrl(新的重定向URL)),则将其加载,然后将finishLoading设置为false.

代码

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

这篇关于Android WebView WebChromeClient:onProgressChanged()中的进度值不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!