如何连接WebViewClient和ChromeWeb View Client

时间:2018-07-08 07:48:24

标签: android webview webviewclient webchromeclient

我已经在Web视图中创建了进度条。因此,加载Web视图后它将消失。现在我想添加error.html页面以不显示任何互联网消息。。我有代码,但是我不知道如何连接他们...帮助我

这是我的代码:

    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            mAdView = findViewById(R.id.adView);
            AdRequest adRequest = new AdRequest.Builder().build();
            mAdView.loadAd(adRequest);

            progressBar = (ProgressBar) findViewById(R.id.progressBar);
            textView = (TextView) findViewById(R.id.tvLoadingPercentage);
            //to hide progressbar after loading part 2
            liProgressContainer = (LinearLayout) findViewById(R.id.liProgressContainer);


            webView = (WebView) findViewById(R.id.webView);
            webView.setWebViewClient(new MyWebViewClient());
            WebSettings browserSetting = webView.getSettings();
            browserSetting.setJavaScriptEnabled(true);
            webView.loadUrl(url);
            webView.setWebChromeClient(new WebChromeClient() {
                public void onProgressChanged(WebView view, int progress) {

                    progressBar.setProgress(progress);
                    textView.setText(progress + " %");
               }
            });
        }

 private class MyWebViewClient extends WebViewClient {


        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            liProgressContainer.setVisibility(View.VISIBLE);
            super.onPageStarted(view, url, favicon);
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            //progressBar.setVisibility(View.VISIBLE);
            view.loadUrl(url);
            return true;
            //return super.shouldOverrideUrlLoading(view, url);
        }

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

            //hide header part
        }
    }

这是error.html代码

 webView.setWebViewClient(new WebViewClient(){
            public void onRecievedError(WebView view ,int errorCode,String description ,String failingUrl)
            {
                webView.loadUrl("file:///android_asset/error.html");
            }
        });

之后

1 个答案:

答案 0 :(得分:1)

用以下代码替换MyWebViewClient:

private class MyWebViewClient extends WebViewClient {


    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        liProgressContainer.setVisibility(View.VISIBLE);
        super.onPageStarted(view, url, favicon);
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        //progressBar.setVisibility(View.VISIBLE);
        view.loadUrl(url);
        return true;
        //return super.shouldOverrideUrlLoading(view, url);
    }

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

        //hide header part
    }

    @Override
    public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
        super.onReceivedError(view, request, error);
        view.loadUrl("file:///android_asset/error.html");
    }
}