WebViewClient 与 WebChromeClient

WebViewClient帮助WebView处理各种通知和请求事件的,我们可以称他为WebView的“内政大臣”。常用的shouldOverrideUrlLoading就是该类的一个方法,比如:

onLoadResource
onPageStart
onPageFinish
onReceiveError
onReceivedHttpAuthRequest

 

WebChromeClient主要辅助WebView处理Javascript的对话框、网站图标、网站标题、加载进度等偏外部事件的“外交大臣”。比如:

onCloseWindow(关闭WebView)
onCreateWindow()
onJsAlert (WebView上alert无效,需要定制WebChromeClient处理弹出)
onJsPrompt
onJsConfirm
onProgressChanged
onReceivedIcon
onReceivedTitle

看上去他们有很多不同,实际使用的话,如果你的WebView只是用来处理一些html的页面内容,只用WebViewClient就行了,如果需要更丰富的处理效果,比如JS、进度条等,就要用到WebChromeClient。

 

 

参考:WebView·开车指南

原文地址:https://www.cnblogs.com/wytiger/p/10470336.html