如何在WebView.loadUrl()方法上添加onCompletionListener?

时间:2018-10-22 07:03:25

标签: android android-webview

WebView现在正在显示,但在此之前出现空白的白色屏幕,这可能是由于加载了网址所致。

// webview to load url
webView.loadUrl("https://www.google.com/");

我想在URL完全加载并且webview准备显示内容时添加onCompletionListener。

3 个答案:

答案 0 :(得分:1)

尝试这个 How can I know that my WebView is loaded 100%?

或者这个网址

https://android--code.blogspot.com/2016/03/android-detect-when-webview-finish.html

 // Set a WebViewClient for WebView
            mWebView.setWebViewClient(new WebViewClient(){
                @Override
                public void onPageStarted(WebView view, String url, Bitmap favicon){
                    // Page loading started
         }

                /*
                    public void onPageFinished (WebView view, String url)
                        Notify the host application that a page has finished loading. This
                        method is called only for main frame. When onPageFinished() is called,
                        the rendering picture may not be updated yet. To get the notification
                        for the new Picture, use onNewPicture(WebView, Picture).

                    Parameters
                        view WebView: The WebView that is initiating the callback.
                        url String: The url of the page.
                */
                @Override
                public void onPageFinished(WebView view, String url){
                    // Page loading finished
                    Toast.makeText(mContext,"Page Loaded.",Toast.LENGTH_SHORT).show();
                }
            });

            // 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){
                    mTextView.setText("Page loading : " + newProgress + "%");

                    if(newProgress == 100){
                        // Page loading finish
                        mTextView.setText("Page Loaded.");
                    }
                }
            });
            // Enable JavaScript
            mWebView.getSettings().setJavaScriptEnabled(true);

            // Load the url in the WebView
            mWebView.loadUrl(mURL);
        }

答案 1 :(得分:0)

您只需要实现WebViewClient并按以下方式使用onPageFinished()

mWebView.setWebViewClient(new WebViewClient() {

   public void onPageFinished(WebView view, String url) {
        // do what you want
    }
});

详细了解this here.

答案 2 :(得分:0)

只需添加一个

 webView.setWebViewClient(new WebViewClient() {

    public void onPageFinished(WebView view, String url) {
        // do whatever you want
    }
});

弗朗西丝here