一、配置权限
<uses-permission android:name="android.permission.INTERNET" />
二、配置WebView
webView.webViewClient = object : WebViewClient() {
override fun shouldOverrideUrlLoading(
view: WebView?,
request: WebResourceRequest?
): Boolean {
view?.let {
it.loadUrl(request?.url.toString())
return true
}
return super.shouldOverrideUrlLoading(view, request)
}
}
val webSettings = webView.settings
webSettings.apply {
loadWithOverviewMode = true
builtInZoomControls = true
javaScriptEnabled = true
useWideViewPort = true
setSupportZoom(true)
javaScriptCanOpenWindowsAutomatically = true
cacheMode = WebSettings.LOAD_DEFAULT
mixedContentMode = WebSettings.MIXED_CONTENT_ALWAYS_ALLOW
setGeolocationEnabled(true)
domStorageEnabled = true
databaseEnabled = true
useWideViewPort = true
allowFileAccess = true
loadWithOverviewMode = true
pluginState = WebSettings.PluginState.ON
cacheMode = WebSettings.LOAD_CACHE_ELSE_NETWORK
}
三、处理问题
因为链接里边需要播放的视频是http链接,而Android9以上系统默认停用明文流量。
Android9或者更高版本
<base-config cleartextTrafficPermitted="false">
<trust-anchors>
<certificates src="system" />
</trust-anchors>
</base-config>
Android7到Android8.1版本
<base-config cleartextTrafficPermitted="true">
<trust-anchors>
<certificates src="system" />
</trust-anchors>
</base-config>
Android6.0及更低版本
<base-config cleartextTrafficPermitted="true">
<trust-anchors>
<certificates src="system" />
<certificates src="user" />
</trust-anchors>
</base-config>
我们需要在res下新建一个xml文件夹,注意命名
然后在文件中添加以下配置:
<?xml version="1.0" encoding="utf-8"?>
<network-security-config xmlns:android="http://schemas.android.com/apk/res/android">ø
<base-config cleartextTrafficPermitted="true" />
</network-security-config>
然后在AndroidManifest里边配置:
<application
...
android:usesCleartextTraffic="true"
android:networkSecurityConfig="@xml/network_security_config">
...
<application>