SetWebViewClient和 SetWebChromeClient的区别

参考;http://blog.csdn.net/dufangyu1990/article/details/39693181

Using WebChromeClient allows you to handle Javascript dialogs, favicons, titles, and the progress. Take a look of this example: Adding alert() support to a WebView
At first glance, there are too many differences WebViewClient & WebChromeClient. But, basically: if you are developing a WebView that won’t require too many features but rendering HTML, you can just use aWebViewClient. On the other hand, if you want to (for instance) load the favicon of the page you are rendering, you should use a WebChromeClient object and override the onReceivedIcon(WebView view, Bitmap icon).
Most of the times, if you don’t want to worry about those things… you can just do this:
webView= (WebView) findViewById(R.id.webview);
webView.setWebChromeClient(new WebChromeClient());
webView.setWebViewClient(new WebViewClient());
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(url);
And your WebView will (in theory) have all features implemented (as the android native browser).

说的很清楚了setWebChromeClient比setWebViewClient功能强大一些,

  1. setWebClient帮助WebView处理各种通知、请求事件

    onLoadResource
    onPageStart
    onPageFinish
    onReceiveError
    onReceivedHttpAuthRequest

如果你不需要太多的功能而仅仅是渲染一个HTML网页,只需要用setWebViewClient就可以了,但是如果要处理比较复杂的事务,就考虑用后者

2.setWebChromeClient辅助WebView处理JavaScript的对话框,网站图标,网站title,加载进度等

onCloseWindow(关闭WebView)
onCreateWindow()
onJsAlert (WebView上alert是弹不出来东西的,需要定制你的WebChromeClient处理弹出)
onJsPrompt
onJsConfirm
onProgressChanged
onReceivedIcon
onReceivedTitle

例如添加进度条:

webview1.setWebChromeClient(new WebChromeClient() 
        {          
            public void onProgressChanged(WebView view, int progress)   
            {   
                setProgress(progress * 100);     
                if(progress == 100){     
                    imageView1.setVisibility(View.GONE); 
                    tv1.setVisibility(View.GONE);
                    pb1.setVisibility(View.GONE);
                    fy1.setVisibility(View.GONE);
                }
            }
        }
        );    

另外如果你怕顾虑太多,可以这样使用

webView= (WebView) findViewById(R.id.webview); 
webView.setWebChromeClient(new WebChromeClient()); 
webView.setWebViewClient(new WebViewClient()); 
webView.getSettings().setJavaScriptEnabled(true); 
webView.loadUrl(url); 
  • 7
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
在 Android Studio 中使用 WebView 调用相机和相册,需要使用 Android 系统提供的 WebChromeClientWebViewClient 类。具体步骤如下: 1. 首先在 AndroidManifest.xml 文件中添加以下权限: ```xml <uses-permission android:name="android.permission.CAMERA" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> ``` 2. 在 Activity 中创建 WebView,并设置 WebChromeClientWebViewClient: ```java WebView webView = findViewById(R.id.webview); webView.setWebChromeClient(new WebChromeClient() { // 处理打开相机或相册的请求 @Override public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) { // 创建打开相机或相册的 Intent Intent intent = fileChooserParams.createIntent(); try { // 启动 Intent startActivityForResult(intent, REQUEST_CODE_FILE_CHOOSER); } catch (ActivityNotFoundException e) { e.printStackTrace(); return false; } return true; } }); webView.setWebViewClient(new WebViewClient() { // 处理网页加载完成事件 @Override public void onPageFinished(WebView view, String url) { super.onPageFinished(view, url); // 在网页中注入 JavaScript 代码,用于调用相机或相册 view.loadUrl("javascript: function chooseImage() {window.android.chooseImage();}"); } }); ``` 3. 在 Activity 中重写 onActivityResult 方法,处理选择图片后的结果: ```java @Override protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { if (requestCode == REQUEST_CODE_FILE_CHOOSER) { if (resultCode == RESULT_OK && data != null) { // 获取选择的图片 Uri Uri uri = data.getData(); if (uri != null) { // 把 Uri 返回给网页 ValueCallback<Uri[]> filePathCallback = mFilePathCallback; if (filePathCallback != null) { filePathCallback.onReceiveValue(new Uri[]{uri}); mFilePathCallback = null; } } } else { // 如果选择图片失败,也要返回结果给网页 ValueCallback<Uri[]> filePathCallback = mFilePathCallback; if (filePathCallback != null) { filePathCallback.onReceiveValue(null); mFilePathCallback = null; } } } } ``` 4. 在 JavaScript 中调用 window.android.chooseImage() 方法,用于打开相机或相册并选择图片: ```javascript function chooseImage() { // 创建 input 元素,用于触发选择图片的操作 var input = document.createElement('input'); input.type = 'file'; input.accept = 'image/*'; input.onchange = function () { // 把选择图片的结果返回给 Android var uri = input.files; if (uri) { window.android.chooseImage(uri); } else { window.android.chooseImage(null); } }; // 触发点击 input 元素的操作,打开相机或相册 input.click(); } ``` 以上就是在 Android Studio 中使用 WebView 调用相机和相册的基本步骤。如果您还有其他问题,请随时提出。
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值