How can I know that my WebView is loaded 100%?

后端 未结 8 1262
我寻月下人不归
我寻月下人不归 2020-11-28 03:46

I\'m trying to load in my WebView some HTML code that contains JavaScript.

Now , I want to test if my WebView is loaded before 5 secondes. I\'ve tried the method

相关标签:
8条回答
  • 2020-11-28 04:19

    As said here: How to listen for a WebView finishing loading a URL?

    boolean loadingFinished = true;
    boolean redirect = false;
    
    mWebView.setWebViewClient(new WebViewClient() {
    
       @Override
       public boolean shouldOverrideUrlLoading(WebView view, String urlNewString) {
           if (!loadingFinished) {
              redirect = true;
           }
    
       loadingFinished = false;
       view.loadUrl(urlNewString);
       return true;
       }
    
       @Override
       public void onPageStarted(WebView view, String url, Bitmap facIcon) {
            loadingFinished = false;
            //SHOW LOADING IF IT ISNT ALREADY VISIBLE  
        }
    
       @Override
       public void onPageFinished(WebView view, String url) {
           if(!redirect){
              loadingFinished = true;
           }
    
           if(loadingFinished && !redirect){
             //HIDE LOADING IT HAS FINISHED
           } else{
              redirect = false; 
           }
    
        }
    });
    
    0 讨论(0)
  • 2020-11-28 04:22

    The below code works perfectly. Once the page has been loaded successfully it will trigger onPageFinished()

            webView.setWebViewClient(new WebViewClient(){
    
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                Toast.makeText(getApplicationContext(),"Started",Toast.LENGTH_SHORT).show();
                super.onPageStarted(view, url, favicon);
            }
    
            @Override
            public void onPageFinished(WebView view, String url) {
                Toast.makeText(getApplicationContext(),"Loaded",Toast.LENGTH_SHORT).show();
                super.onPageFinished(view, url);
            }
        });
    
    0 讨论(0)
  • 2020-11-28 04:26
    webView.setWebChromeClient(new WebChromeClient() {
                public void onProgressChanged(WebView view, int progress) {
                    if (progress == 100) {
                        //do your task
    
                    }
                }
            });
    
    0 讨论(0)
  • 2020-11-28 04:32

    You can try to extend WebChromeClient, override onProgressChanged(WebView view, int newProgress) and register it on your WebView with setWebChromeClient(WebChromeClient) method. This will free your application from the additional thread that you are starting just to check whether progress changed. Use the callback, it is simpler. So that would be the first thing.

    Another one is that I was also experimenting with this progress status and I came to some conclusions about how it behaves:

    • for some use cases (such as check if page is even under the given url) it has to be greater then 10. When WebView makes the connection to the url provided then it automatically sets the progress value to 10 even if it did not make a successful connection, if it is greater then 10, then you can be sure that url could be accessed and the loading has begun,
    • progress will be returned as 100% when you call stopLoading() on your WebView,
    • keeping previous point in mind also when WebView won't be able to load the full site (it will get a timeout for an image for example) then it will report that page was fully loaded (100%)

    To sum up, this progress bar is an indicator on whether WebView has finished loading the site or not but in terms of WebKit not in terms of page being completely downloaded. You have to keep in mind that connection may crash, resources (images, css, js) may not load for some reason, JavaScript can load some more resources when page will finish up loading etc. this progress can't tell you if the sites content was fully loaded or not, it tells you that WebView thinks that this should be all.

    I have no other ideas on how to check whether page was fully loaded or not, I think this is the only way.

    0 讨论(0)
  • 2020-11-28 04:34

    The best way to detect if a page has rendered is to use the onPageCommitVisible callback, available from API 23. onPageLoadFinished is not suitable, since it's delivered too soon (when the HTML is processed, but not yet rendered).

    webview.setWebViewClient(new WebViewClient() {
    
        @Override
         public void onPageCommitVisible (WebView view, 
                String url)
        }
    }
    
    0 讨论(0)
  • 2020-11-28 04:34

    Set a WebChromeClient for WebView Another way to determine when page loading finish

                mWebView.setWebChromeClient(new WebChromeClient(){
                    /*public void onProgressChanged (WebView view, int newProgress)           Tell the host application the current progress of loading a page.
    
                        Parameters
                            view WebView: The WebView that initiated the callback.
    
                            newProgress int: Current page loading progress, represented by an
                                integer between 0 and 100.
                    */
                    public void onProgressChanged(WebView view, int newProgress){
                        Toast.makeText(getActivity().getApplicationContext(),"Page loading : " + newProgress + "%",Toast.LENGTH_SHORT).show();
    
                if(newProgress == 100){
                    // Page loading finish
                    Toast.makeText(getActivity().getApplicationContext(),"Page loaded",Toast.LENGTH_SHORT).show();
                }
                    }
                });
    
    0 讨论(0)
提交回复
热议问题