Are WebViewClient and WebChromeClient mutually exclusive?

本秂侑毒 提交于 2019-11-28 03:14:55

You certainly can use both, they just have different functions. Setting your own custom WebViewClient lets you handle onPageFinished, shouldOverrideUrlLoading, etc., WebChromeClient lets you handle Javascript's alert() and other functions.

Just make your own class, for example:

public class MyWebChromeClient extends WebChromeClient {
    //Handle javascript alerts:
    @Override
public boolean onJsAlert(WebView view, String url, String message, final android.webkit.JsResult result)  
{
  Log.d("alert", message);
  Toast.makeText(context, message, 3000).show();
  result.confirm();
  return true;
};
...

and / or

public class MyWebViewClient extends WebViewClient {
@Override
    //Run script on every page, similar to Greasemonkey:
public void onPageFinished(WebView view, String url) {
        view.loadUrl("javascript:alert('hi')");
    }
...

Just override the functions described in the documentation, then set your client in onCreate with:

webview.setWebViewClient(new MyWebViewClient());
webview.setWebChromeClient(new MyWebChromeClient());
Tomasz Dzieniak

You can use both clients simultaneously. It is useful as both of them provides different functionalities.

For example, if you would like to:

  • follow redirects in web view rather than opening available browser

and

  • trace the loading progress

which are impossible with just one of the clients you can do the following:

WebView webView = (WebView) findViewById(R.id.web_view);
webView.setWebViewClient(new WebViewClient());
webView.setWebChromeClient(new WebChromeClient() {
    @Override
    public void onProgressChanged(WebView view, int newProgress) {
        super.onProgressChanged(view, newProgress);

        // Your custom code.
    }
});

As the default implementation of shouldOverrideUrlLoading(WebView, String) method can be use as-is for above mentioned case - there is no need to override it unless you would like other behavior.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!