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

后端 未结 8 1263
我寻月下人不归
我寻月下人不归 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:37

    Above solutions did not work for me, I wanted to make sure I have page loaded 100% and then inject javascript to do some intensive research on the page. I came up with following solution with little deviation from the solution provided by NeTeInStEiN. This may not work for everyone but it worked for me. Again it depends on what you are trying to achieve from the finished page.

    String url; is from your activity which want to load this url
        private class ExtendedWebViewClient extends WebViewClient {
            private int webViewPreviousState;
            private final int PAGE_STARTED = 0x1;
            private final int PAGE_REDIRECTED = 0x2;
            public void onPageStarted(WebView paramWebView, String paramString,
                    Bitmap paramBitmap) {
                super.onPageStarted(paramWebView, paramString, paramBitmap);
                if(paramString.contentEquals(url)) {
                    webViewPreviousState = PAGE_STARTED;
                } else {
                    webViewPreviousState = PAGE_REDIRECTED;
                }
    // DO YOU STUFF IF NEEDED
    }
            public void onPageFinished(WebView paramWebView, String paramString) {
                if (webViewPreviousState == PAGE_STARTED) {
    // I AM GETTING HERE WHEN MY PAGE IS LOADED 100%
    // NOW ITS TIME FOR ME TO RUN JAVASCRIPT FUNCTIONS
    // DO YOUR STUFF
    }
    }
    

    Hope This helps.

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

    You need to overwrite the onPageFinished of WebViewClient

    Here is an example for you

    private class Callback extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    
    
        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            //do what you want to do
    
            }
    
    
    }
    
    0 讨论(0)
提交回复
热议问题