浅谈WebViewClient与WebChromeClient

简介:WebViewClient被用来传递单纯的加载一个链接时所发生的事件,比如开始加载,结束加载等,它代表这个链接加载时的最普通的和最笼统的事件,WebChromeClient更多的是传递JS对话框,上传文件,网页的标题改变等网页内元素的事件。


 

 

 一,WebViewClient

 

     

  1. 下面介绍下几个简单的应用:

         

  •        使用我们自己的WebView加载页面上的链接

               当用户点击一个网页上的连接时,默认的操作是另外打开一个与之相关的应用然后传递URL,通常是打开我们系统默认的浏览器然后加载目标URL地址。                  但是我们可以重新定义这个操作既使用我们自己的WebView打开打开这个URL,你还可以通过他们的浏览历史允许用户导航向前像后。能够到达上面的效                果西需要我们提供一个自己的WebView,然后适用setWebViewClient()

 

WebView webview = f(WebView)indViewById(R.id.webview);webview.setWebViewClient(new WebViewClient());

 

               这个运行用户使用你自己的WebView加载链接

               如果想要在点击一个连接后获取到更多的控制,需要我们创建我们自己的WebClient,然后重写shouldOverrideUrlLoading() 方法,

               例如:我们如果点击的我链接是我自己的就用我们自己的WebView加载,如果是别的链接就用别的应用打开

 

private class MyWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
       
        if (Uri.parse(url).getHost().equals("www.example.com")) {
            // This is my web site, so do not override; let my WebView 
               load  the page
            return false;
        }
        // Otherwise, the link is not for a page on my site, so launch 
            another Activity that handles URLs
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        startActivity(intent);
        return true;
    }
}

然后:

WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.setWebViewClient(new MyWebViewClient());

showOverrideUrlLoading的返回值为false时代表我们自己的WebView会处理这个链接,如果为true代表会传递给系统做相应的处理(如:提供一些能够处理这个连接的应用让你选择)

 

  • 导航网页浏览历史

        当我们重写URL加载时,它会自动的累计你的网页浏览历史,你可以通过goBack()和goForward()向前或者向后导航。

        例如:我们的Activity使用返回键处理返回导航

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    // Check if the key event was the Back button and if there's history
    if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {
        myWebView.goBack();
        return true;
    }
    // If it wasn't the Back key or there's no web page history, bubble up to the default
    // system behavior (probably exit the activity)
    return super.onKeyDown(keyCode, event);
}

如果不进行这个检查,当用户已经浏览器完所有的历史之后goBack()和goForward()将不起任何作用。

 

 

二,WebChromeClient

 

       WebChromeClient是Html/Js和Android客户端进行交互的一个中间件,其将webview中js所产生的事件封装,然后传递到Android客户端。Google这样做的其中一个很重要的原因就是安全问题。

      主要辅助WebView处理JavaScript对话框,加载进度,上传文件等。

 

1.下面是几个常使用的案例

 

  • 使用html与Android系统交互选择并上传系统的文件

         这里有两个方法:

        第一种:安卓客户端远程调用html中JS方法,js远程调用Android客户端方法。

        第二种:重写WebChromeClient

        详情请参考我的另一篇文章:http://www.cnblogs.com/ufreedom/p/4158081.html

  • 调试WebView,打印WebView处理网页时log

         WebChromeClient处理了javascript的console的功能,js可以使用console打印调试信息,在API 7时WebChromeClient将console信息通过      onConsoleMessage传递给java层。在API8或者更高的版本中WebChromeClient将console信息封装成ConsoleMessage,然后通过onChromClient传递。

API7:

​WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.setWebChromeClient(new WebChromeClient() {
  public void onConsoleMessage(String message, int lineNumber, String sourceID) {
    Log.d("MyApplication", message + " -- From line "
                         + lineNumber + " of "
                         + sourceID);
  }
});

 

API8或者更高:

WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.setWebChromeClient(new WebChromeClient() {
  public boolean onConsoleMessage(ConsoleMessage cm) {
    Log.d("MyApplication", cm.message() + " -- From line "
                         + cm.lineNumber() + " of "
                         + cm.sourceId() );
    return true;
  }
});

 

ConsoleMessage包含了一个MesssageLeavel 对象用来表示console信息的类型,我们可以使用messageLevel()获取信息类型。

 

  • WebChromeClient弹出html中js对话框信息

        我们知道WebView是无法弹出js对话框的,WebChromeClient封装了js对话框信息

  

        @Override
        public boolean onJsAlert(WebView view, String url, String message, final JsResult result) {
            String newTitle = getTitleFromUrl(url);

            new AlertDialog.Builder(DuomiWebActivity.this).setTitle(newTitle).setMessage(message)
                    .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            result.confirm();
                        }
                    }).setCancelable(false).create().show();
            return true;
        }

        @Override
        public boolean onJsConfirm(WebView view, String url, String message, final JsResult result) {

            String newTitle = getTitleFromUrl(url);

            new AlertDialog.Builder(DuomiWebActivity.this).setTitle(newTitle).setMessage(message)
                    .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            result.confirm();
                        }
                    }).setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            result.cancel();
                        }
                    }).setCancelable(false).create().show();
            return true;

        }

 

   

 

  •  添加网页加载进度条效果

         需要我们重写onProgressChanged(WebView view, int newProgress)方法

 

posted @ 2014-12-25 11:14  UFreedom  阅读(1802)  评论(0编辑  收藏  举报