WebView中WebViewClient和WebChromeClient积累

相信现在WebView使用的相当频繁,遇到的问题也很多,今天我就来解析一下WebView的WebViewClient和WebChromeClient,表面文字看着以为是内核不一样。其实不是,WebViewClient用于帮助WebView处理各种通知和请求事件;而WebChromeClient更丰富一些,帮助WebView处理Javascript各类对话框、弹窗等等。

WebViewClient

以下都是WebViewClient比较基础的方法

//处理webView的按键事件
boolean shouldOverrideKeyEvent(WebView view, KeyEvent event)
//截取url请求,在当前视图加载,避免在跳转到自带浏览器
boolean shouldOverrideUrlLoading(WebView view, String url)
//WebView改变时调用
void onScaleChanged(WebView view, float oldScale, float newScale)
//对https的请求处理
void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error)
//获取返回信息授权
void onReceivedHttpAuthRequest(WebView view, HttpAuthHandler handler, String host, String realm)
//处理报错信息(API23以后用第二个)
void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error)
//页面开始载入时调用
void onPageStarted(WebView view, String url, Bitmap favicon)
//页面载入结束时调用
void onPageFinished(WebView view, String url)
//加载资源时调用
void onLoadResource(WebView view, String url)

WebChromeClient

以下是WebChromeClient比较基础的方法

//webview关闭时调用
void onCloseWindow(WebView window)
//Js的弹窗
boolean onJsAlert(WebView view, String url, String message, JsResult result)
//Js提示框
boolean onJsPrompt(WebView view, String url, String message, String defaultValue, JsPromptResult result)
//Js确认框
boolean onJsConfirm(WebView view, String url, String message, JsResult result)
//加载进度
void onProgressChanged(WebView view, int newProgress)
//全屏模式(API18以后用第二种)
onShowCustomView(View view, WebChromeClient.CustomViewCallback callback)
onShowCustomView(View view, int requestedOrientation, WebChromeClient.CustomViewCallback callback)

file标签 打开文件

以上都是比较简单的一下开放重写的方法,也会有一些未开放的重写方法,比如如下:

// For Android 3.0+ 
public void openFileChooser(ValueCallback uploadMsg) {...} 
// For Android 3.0+ 
public void openFileChooser(ValueCallback uploadMsg, String acceptType) {...} 
// For Android 4.1 
public void openFileChooser(ValueCallback uploadMsg, String acceptType, String capture) {...}
// For Android 5.0+
public boolean onShowFileChooser (WebView webView, ValueCallback filePathCallback, WebChromeClient.FileChooserParams fileChooserParams) {
return true;  
}

好了,今天就先到这里,以上都是我使用中的一些积累,跟大家相互分享了。

你可能感兴趣的:(WebView中WebViewClient和WebChromeClient积累)