如何在WebView开始加载新页面时收到通知?

时间:2017-12-25 09:16:09

标签: android webview

我知道如何在加载网页时收到通知,但有没有办法知道它何时开始加载从原始网页链接启动的新网页?

我想要它的原因是在页面开始加载时使ProgressBar可见,并在它完成时使其不可见。

更新正如我所说,我知道如何在加载网页时收到通知。我现在要问的是,是否可以知道新页面何时开始加载页面。虽然从链接中我发现onPageStarted,但它确实有用。

2 个答案:

答案 0 :(得分:0)

请查看以下

 webView.setWebViewClient(new WebViewClient() {      


        //If you will not use this method url links are opeen in new brower not in webview
        public boolean shouldOverrideUrlLoading(WebView view, String url) {              
            view.loadUrl(url);
            return true;
        }


        public void onLoadResource (WebView view, String url) {
          //Show loader on url load
        }

        public void onPageFinished(WebView view, String url) {
           //cancel loader complted
        }

    }); 

     // Set Javascript enabled on webview  
    webView.getSettings().setJavaScriptEnabled(true); 

答案 1 :(得分:0)

要收听开始加载的网页,您应该覆盖onPageStarted,要听取结束,您应该覆盖onPageFinished 并且为了使其更完整,请使用onReceivedError

侦听错误
webView.setWebViewClient(new WebViewClient() {  

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon){
            super.onPageStarted(view, url, favicon);

            // runs when a page starts loading
            progressBar.setVisibility(View.VISIBLE);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);

            // page finishes loading
            progressBar.setVisibility(View.GONE);
        }

        @Override
        public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
            super.onReceivedError(view, request, error);

            // runs when there's a failure in loading page
            progressBar.setVisibility(View.GONE);
            Toast.makeText(context, "Failure on loading web page", Toast.LENGTH_SHORT).show();
        }
});