|
|
@@ -2,13 +2,12 @@ package com.yingyangfly.webview
|
|
|
|
|
|
import android.annotation.SuppressLint
|
|
|
import android.content.pm.ActivityInfo
|
|
|
-import android.graphics.Bitmap
|
|
|
import android.os.Build
|
|
|
-import android.webkit.WebResourceRequest
|
|
|
-import android.webkit.WebSettings
|
|
|
-import android.webkit.WebView
|
|
|
-import android.webkit.WebViewClient
|
|
|
+import android.util.Log
|
|
|
+import android.webkit.*
|
|
|
import com.alibaba.android.arouter.facade.annotation.Route
|
|
|
+import com.github.lzyzsd.jsbridge.BridgeHandler
|
|
|
+import com.github.lzyzsd.jsbridge.CallBackFunction
|
|
|
import com.yingyangfly.baselib.base.BaseActivity
|
|
|
import com.yingyangfly.baselib.ext.toast
|
|
|
import com.yingyangfly.baselib.router.RouterUrlCommon
|
|
|
@@ -24,7 +23,7 @@ class BridgeWebActivity : BaseActivity<ActivityBridgeWebBinding>() {
|
|
|
var url: String = ""
|
|
|
|
|
|
override fun initViews() {
|
|
|
- url = "http://60.205.201.7/cocos/mobile/whacaMole/"
|
|
|
+ url = "http://60.205.201.7/cocos/mobile/aoYouTaiKong/"
|
|
|
initWebView()
|
|
|
}
|
|
|
|
|
|
@@ -36,7 +35,7 @@ class BridgeWebActivity : BaseActivity<ActivityBridgeWebBinding>() {
|
|
|
|
|
|
}
|
|
|
|
|
|
- @SuppressLint("JavascriptInterface")
|
|
|
+ @SuppressLint("JavascriptInterface", "SetJavaScriptEnabled")
|
|
|
private fun initWebView() {
|
|
|
webSettings = binding.webView.settings
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
|
|
@@ -48,6 +47,7 @@ class BridgeWebActivity : BaseActivity<ActivityBridgeWebBinding>() {
|
|
|
webSettings.javaScriptCanOpenWindowsAutomatically = true
|
|
|
webSettings.javaScriptEnabled = true
|
|
|
webSettings.setAppCacheEnabled(true)
|
|
|
+ webSettings.allowUniversalAccessFromFileURLs = true
|
|
|
webSettings.cacheMode = WebSettings.LOAD_DEFAULT
|
|
|
/**必须的设置, 访问网页版的H5,一定要设置。该方法是设置支持DomStorage,
|
|
|
* DOM Storage 分为 sessionStorage 和 localStorage。
|
|
|
@@ -56,9 +56,18 @@ class BridgeWebActivity : BaseActivity<ActivityBridgeWebBinding>() {
|
|
|
*/
|
|
|
webSettings.domStorageEnabled = true
|
|
|
// 通过addJavascriptInterface()将Java对象映射到JS对象 下面一行代码是 JS调用原生方法
|
|
|
- binding.webView.addJavascriptInterface(AndroidToJs(this), "callbackHandle")
|
|
|
- binding.webView.addJavascriptInterface(AndroidToJs(this), "callbackImgHandle")
|
|
|
- binding.webView.addJavascriptInterface(AndroidToJs(this), "callbackBackHandle")
|
|
|
+// binding.webView.addJavascriptInterface(AndroidToJs(this), "callJavaFunc")
|
|
|
+// binding.webView.callHandler("javascript:callJavaFunc()"
|
|
|
+// ) { value -> Log.e("wpp", "----------------------------" + value) }
|
|
|
+
|
|
|
+ binding.webView.registerHandler("", object : BridgeHandler{
|
|
|
+ override fun handler(data: String?, function: CallBackFunction?) {
|
|
|
+
|
|
|
+ }
|
|
|
+ })
|
|
|
+
|
|
|
+// binding.webView.addJavascriptInterface(AndroidToJs(this), "callbackImgHandle")
|
|
|
+// binding.webView.addJavascriptInterface(AndroidToJs(this), "callbackBackHandle")
|
|
|
|
|
|
binding.webView.isDrawingCacheEnabled = true
|
|
|
binding.webView.buildDrawingCache()
|
|
|
@@ -72,16 +81,7 @@ class BridgeWebActivity : BaseActivity<ActivityBridgeWebBinding>() {
|
|
|
url.let {
|
|
|
view?.loadUrl(it)
|
|
|
}
|
|
|
- return super.shouldOverrideUrlLoading(view, request)
|
|
|
- }
|
|
|
-
|
|
|
- override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
|
|
|
- super.onPageStarted(view, url, favicon)
|
|
|
- }
|
|
|
-
|
|
|
- override fun onPageFinished(view: WebView?, url: String?) {
|
|
|
- super.onPageFinished(view, url)
|
|
|
-
|
|
|
+ return true
|
|
|
}
|
|
|
}
|
|
|
if (url.isNullOrEmpty().not()) {
|
|
|
@@ -113,4 +113,9 @@ class BridgeWebActivity : BaseActivity<ActivityBridgeWebBinding>() {
|
|
|
}
|
|
|
super.onResume()
|
|
|
}
|
|
|
+
|
|
|
+ @JavascriptInterface
|
|
|
+ fun callbackHandle(){
|
|
|
+ Log.e("wpp", "-----------------------")
|
|
|
+ }
|
|
|
}
|