|
@@ -1,6 +1,7 @@
|
|
|
package com.yingyangfly.webview
|
|
|
|
|
|
import android.content.pm.ActivityInfo
|
|
|
+import android.graphics.Bitmap
|
|
|
import android.os.Build
|
|
|
import android.webkit.WebResourceRequest
|
|
|
import android.webkit.WebSettings
|
|
@@ -19,7 +20,10 @@ class WebviewActivity : BaseActivity<ActivityWebviewBinding>() {
|
|
|
|
|
|
var webSettings: WebSettings? = null
|
|
|
|
|
|
+ var url: String? = null
|
|
|
+
|
|
|
override fun initViews() {
|
|
|
+ url = "http://60.205.201.7/cocos/mobile/whacaMole/"
|
|
|
webSettings = binding.web.settings
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
|
|
webSettings?.mixedContentMode = WebSettings.MIXED_CONTENT_ALWAYS_ALLOW
|
|
@@ -37,6 +41,12 @@ class WebviewActivity : BaseActivity<ActivityWebviewBinding>() {
|
|
|
* sessionStorage 用来存储与页面相关的数据,它在页面关闭后无法使用。而 localStorage 则持久存在,在页面关闭后也可以使用。
|
|
|
*/
|
|
|
webSettings?.domStorageEnabled = true
|
|
|
+
|
|
|
+ // 通过addJavascriptInterface()将Java对象映射到JS对象 下面一行代码是 JS调用原生方法
|
|
|
+ binding.web.addJavascriptInterface(AndroidToJs(this), "callbackHandle")
|
|
|
+ binding.web.addJavascriptInterface(AndroidToJs(this), "callbackImgHandle")
|
|
|
+ binding.web.addJavascriptInterface(AndroidToJs(this), "callbackBackHandle")
|
|
|
+
|
|
|
binding.web.isDrawingCacheEnabled = true
|
|
|
binding.web.buildDrawingCache()
|
|
|
binding.web.buildLayer()
|
|
@@ -56,23 +66,26 @@ class WebviewActivity : BaseActivity<ActivityWebviewBinding>() {
|
|
|
binding.web.settings.setAppCacheEnabled(true)
|
|
|
// 是否使用缓存
|
|
|
binding.web.settings.domStorageEnabled = true//DOM Storage
|
|
|
- //访问网页
|
|
|
- binding.web.loadUrl("http://60.205.201.7/cocos/mobile/whacaMole/")
|
|
|
//系统默认会通过手机浏览器打开网页,为了能够直接通过web显示网页,则必须设置
|
|
|
binding.web.webViewClient = object : WebViewClient() {
|
|
|
- override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
|
|
|
- view?.loadUrl(url!!)
|
|
|
- //返回true
|
|
|
- return true
|
|
|
- }
|
|
|
-
|
|
|
override fun shouldOverrideUrlLoading(
|
|
|
view: WebView?,
|
|
|
request: WebResourceRequest?
|
|
|
): Boolean {
|
|
|
- return false
|
|
|
+ url?.let {
|
|
|
+ view?.loadUrl(it)
|
|
|
+ }
|
|
|
+ return super.shouldOverrideUrlLoading(view, request)
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
|
|
|
+ super.onPageStarted(view, url, favicon)
|
|
|
}
|
|
|
}
|
|
|
+ if (url.isNullOrEmpty().not()) {
|
|
|
+ //访问网页
|
|
|
+ binding.web.loadUrl(url!!)
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
override fun initListener() {
|