How to listen for a WebView finishing loading a URL?

前端 未结 17 731
予麋鹿
予麋鹿 2020-11-22 05:40

I have a WebView that is loading a page from the Internet. I want to show a ProgressBar until the loading is complete.

How do I listen for

相关标签:
17条回答
  • 2020-11-22 06:22

    I am pretty partial to @NeTeInStEiN (and @polen) solution but would have implemented it with a counter instead of multiple booleans or state watchers (just another flavor but I thought might share). It does have a JS nuance about it but I feel the logic is a little easier to understand.

    private void setupWebViewClient() {
        webView.setWebViewClient(new WebViewClient() {
            private int running = 0; // Could be public if you want a timer to check.
    
            @Override
            public boolean shouldOverrideUrlLoading(WebView webView, String urlNewString) {
                running++;
                webView.loadUrl(urlNewString);
                return true;
            }
    
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                running = Math.max(running, 1); // First request move it to 1.
            }
    
            @Override
            public void onPageFinished(WebView view, String url) {
                if(--running == 0) { // just "running--;" if you add a timer.
                    // TODO: finished... if you want to fire a method.
                }
            }
        });
    }
    
    0 讨论(0)
  • 2020-11-22 06:26

    for Kotlin users:

    webView.webViewClient = object : WebViewClient() {
                override fun onPageFinished(view: WebView?, url: String?) {
                    // do your logic
                }
            }
    

    there are a lot of methods that you can override though

    0 讨论(0)
  • 2020-11-22 06:30

    Extend WebViewClient and call onPageFinished() as follows:

    mWebView.setWebViewClient(new WebViewClient() {
    
       public void onPageFinished(WebView view, String url) {
            // do your stuff here
        }
    });
    
    0 讨论(0)
  • 2020-11-22 06:30

    this will been called before he start loading the page (and get the same parameters as onFinished())

    @Override
    public void onPageCommitVisible(WebView view, String url) {
       super.onPageCommitVisible(view, url);
    }
    
    0 讨论(0)
  • 2020-11-22 06:31

    You can trace the Progress Staus by the getProgress method in webview class.

    Initialize the progress status

    private int mProgressStatus = 0;
    

    then the AsyncTask for loading like this:

    private class Task_News_ArticleView extends AsyncTask<Void, Void, Void> {
        private final ProgressDialog dialog = new ProgressDialog(
                your_class.this);
    
        // can use UI thread here
        protected void onPreExecute() {
            this.dialog.setMessage("Loading...");
            this.dialog.setCancelable(false);
            this.dialog.show();
        }
    
        @Override
        protected Void doInBackground(Void... params) {
            try {
                while (mProgressStatus < 100) {
                    mProgressStatus = webview.getProgress();
    
                }
            } catch (Exception e) {
    
            }
            return null;
    
        }
    
        protected void onPostExecute(Void result) {
            if (this.dialog.isShowing()) {
                this.dialog.dismiss();
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题