Android WebView 的相关设置
settings相关设置
webView.settings.apply {
javaScriptEnabled = true
layoutAlgorithm = WebSettings.LayoutAlgorithm.NARROW_COLUMNS
useWideViewPort = true
savePassword = true
saveFormData = true
databaseEnabled = true
domStorageEnabled = true
cacheMode = WebSettings.LOAD_NO_CACHE
setAppCachePath(cacheDir.absolutePath)
displayZoomControls = true
layoutAlgorithm = WebSettings.LayoutAlgorithm.SINGLE_COLUMN
useWideViewPort = true
loadWithOverviewMode = true
setSupportZoom(true)
builtInZoomControls = true
blockNetworkImage = true
loadsImagesAutomatically = true
standardFontFamily = ""
minimumFontSize = 8
defaultTextEncodingName = "UTF-8"
allowFileAccess = true
textZoom = 100
pluginState = WebSettings.PluginState.ON
setRenderPriority(WebSettings.RenderPriority.HIGH)
supportMultipleWindows()
setNeedInitialFocus(true)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
WebView.setWebContentsDebuggingEnabled(true)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mixedContentMode = WebSettings.LOAD_NORMAL
}
}
userAgentString += "/native_$packageName"
}
WebClient相关设置
webView.webViewClient = object: WebViewClient(){
override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
super.onPageStarted(view, url, favicon)
Log.i("webViewClient" , "页面开始加载")
}
override fun onPageFinished(view: WebView?, url: String?) {
super.onPageFinished(view, url)
Log.i("webViewClient" , "页面加载完成回调")
}
override fun onLoadResource(view: WebView?, url: String?) {
super.onLoadResource(view, url)
Log.i("webViewClient" , "加载Url资源回调")
}
override fun onReceivedError(
view: WebView?,
request: WebResourceRequest?,
error: WebResourceError?
) {
super.onReceivedError(view, request, error)
Log.i("webViewClient" , "访问地址错误回调")
}
override fun onReceivedSslError(
view: WebView?,
handler: SslErrorHandler?,
error: SslError?
) {
super.onReceivedSslError(view, handler, error)
Log.i("webViewClient" , "加载SSl错误回调")
}
}
webChromeClient 相关设置
webView.webChromeClient = object : WebChromeClient(){
override fun onJsAlert(
view: WebView?,
url: String?,
message: String?,
result: JsResult?
): Boolean {
Log.i("webChromeClient" , "当网页调用alert()来弹出alert弹出框前回调,用以拦截alert()函数")
return super.onJsAlert(view, url, message, result)
}
override fun onJsConfirm(
view: WebView?,
url: String?,
message: String?,
result: JsResult?
): Boolean {
Log.i("webChromeClient" , "当网页调用confirm()来弹出confirm弹出框前回调,用以拦截confirm()函数")
return super.onJsConfirm(view, url, message, result)
}
override fun onJsPrompt(
view: WebView?,
url: String?,
message: String?,
defaultValue: String?,
result: JsPromptResult?
): Boolean {
Log.i("webChromeClient" , "当网页调用prompt()来弹出prompt弹出框前回调,用以拦截prompt()函数")
return super.onJsPrompt(view, url, message, defaultValue, result)
}
override fun onReceivedTitle(view: WebView?, title: String?) {
Log.i("webChromeClient" , "网页的title回调")
super.onReceivedTitle(view, title)
}
override fun onReceivedIcon(view: WebView?, icon: Bitmap?) {
Log.i("webChromeClient" , "网页的icon回调")
super.onReceivedIcon(view, icon)
}
override fun onShowFileChooser(
webView: WebView?,
filePathCallback: ValueCallback<Array<Uri>>?,
fileChooserParams: FileChooserParams?
): Boolean {
Log.i("webChromeClient" , "文件选择回调")
return super.onShowFileChooser(webView, filePathCallback, fileChooserParams)
}
override fun onConsoleMessage(consoleMessage: ConsoleMessage?): Boolean {
Log.i("webChromeClient" , "打印 console 信息")
return super.onConsoleMessage(consoleMessage)
}
override fun onProgressChanged(view: WebView?, newProgress: Int) {
super.onProgressChanged(view, newProgress)
Log.i("webChromeClient" , "通知程序当前页面加载进度")
}
}
|