|
|
@@ -0,0 +1,116 @@
|
|
|
+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 com.alibaba.android.arouter.facade.annotation.Route
|
|
|
+import com.yingyangfly.baselib.base.BaseActivity
|
|
|
+import com.yingyangfly.baselib.ext.toast
|
|
|
+import com.yingyangfly.baselib.router.RouterUrlCommon
|
|
|
+import com.yingyangfly.webview.databinding.ActivityBridgeWebBinding
|
|
|
+
|
|
|
+/**
|
|
|
+ * BridgeWebview
|
|
|
+ */
|
|
|
+@Route(path = RouterUrlCommon.WEB_VIEW_INTERACTION_JS)
|
|
|
+class BridgeWebActivity : BaseActivity<ActivityBridgeWebBinding>() {
|
|
|
+
|
|
|
+ private lateinit var webSettings: WebSettings
|
|
|
+ var url: String = ""
|
|
|
+
|
|
|
+ override fun initViews() {
|
|
|
+ url = "http://60.205.201.7/cocos/mobile/whacaMole/"
|
|
|
+ initWebView()
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun initListener() {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun initData() {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @SuppressLint("JavascriptInterface")
|
|
|
+ private fun initWebView() {
|
|
|
+ webSettings = binding.webView.settings
|
|
|
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
|
|
+ webSettings.mixedContentMode = WebSettings.MIXED_CONTENT_ALWAYS_ALLOW
|
|
|
+ }
|
|
|
+ val ua = webSettings.userAgentString
|
|
|
+ //必须设置
|
|
|
+ webSettings.userAgentString = "$ua; app/lottchina Android"
|
|
|
+ webSettings.javaScriptCanOpenWindowsAutomatically = true
|
|
|
+ webSettings.javaScriptEnabled = true
|
|
|
+ webSettings.setAppCacheEnabled(true)
|
|
|
+ webSettings.cacheMode = WebSettings.LOAD_DEFAULT
|
|
|
+ /**必须的设置, 访问网页版的H5,一定要设置。该方法是设置支持DomStorage,
|
|
|
+ * DOM Storage 分为 sessionStorage 和 localStorage。
|
|
|
+ * localStorage 对象和 sessionStorage 对象使用方法基本相同,它们的区别在于作用的范围不同。
|
|
|
+ * sessionStorage 用来存储与页面相关的数据,它在页面关闭后无法使用。而 localStorage 则持久存在,在页面关闭后也可以使用。
|
|
|
+ */
|
|
|
+ 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.isDrawingCacheEnabled = true
|
|
|
+ binding.webView.buildDrawingCache()
|
|
|
+ binding.webView.buildLayer()
|
|
|
+
|
|
|
+ binding.webView.webViewClient = object : WebViewClient() {
|
|
|
+ override fun shouldOverrideUrlLoading(
|
|
|
+ view: WebView?,
|
|
|
+ request: WebResourceRequest?
|
|
|
+ ): Boolean {
|
|
|
+ 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)
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (url.isNullOrEmpty().not()) {
|
|
|
+ binding.webView.loadUrl(url)
|
|
|
+ } else {
|
|
|
+ "视频链接不能为空".toast()
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun onDestroy() {
|
|
|
+ destoryWebView()
|
|
|
+ super.onDestroy()
|
|
|
+ }
|
|
|
+
|
|
|
+ fun destoryWebView() {
|
|
|
+ binding.webView.stopLoading() // 停止加载
|
|
|
+ binding.webView.removeAllViews() // 移除webview上子view
|
|
|
+ binding.webView.clearCache(true) // 清除缓存
|
|
|
+ binding.webView.clearHistory() // 清楚历史
|
|
|
+ binding.webView.destroy() // 销毁WebView自身。
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun onResume() {
|
|
|
+ /**
|
|
|
+ * 设置为横屏
|
|
|
+ */
|
|
|
+ if (requestedOrientation != ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
|
|
|
+ requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
|
|
|
+ }
|
|
|
+ super.onResume()
|
|
|
+ }
|
|
|
+}
|