WebView与H5交互
程序员文章站
2022-03-11 08:17:40
val settings = rm_webview.settingssettings.javaScriptEnabled = truesettings.domStorageEnabled = truesettings.javaScriptCanOpenWindowsAutomatically = truesettings.cacheMode = WebSettings.LOAD_DEFAULTsettings.setAppCacheEnabled(true)settings.setSuppor....
val settings = rm_webview.settings settings.javaScriptEnabled = true settings.domStorageEnabled = true settings.javaScriptCanOpenWindowsAutomatically = true settings.cacheMode = WebSettings.LOAD_DEFAULT settings.setAppCacheEnabled(true) settings.setSupportZoom(true) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { settings.mixedContentMode = WebSettings.MIXED_CONTENT_ALWAYS_ALLOW } settings.useWideViewPort = true settings.loadWithOverviewMode = true rm_webview.loadUrl(load_url + h5_url) rm_webview.webViewClient = object : WebViewClient() { override fun onReceivedSslError( view: WebView?, handler: SslErrorHandler?, error: SslError? ) { handler?.cancel() } override fun onPageFinished(view: WebView?, url: String?) { super.onPageFinished(view, url) val userData = UserDataUtlis().getUserData() if (userData != null) { if (type == 2) { initData( "{}" ) } } } } rm_webview.webChromeClient = object : WebChromeClient() { override fun onReceivedTitle(view: WebView?, title: String?) { super.onReceivedTitle(view, title) if (title != null) { app_title_tv.text = title } } } rm_webview.addJavascriptInterface(jsObject, "androidObject")
@Suppress("NewApi") private fun initData(jsonData: String) { if (Build.VERSION.SDK_INT < 18) { rm_webview.loadUrl("javascript:initData($jsonData)\"") } else { rm_webview.evaluateJavascript("javascript:initData($jsonData)") { } } }
/** * 返回上一级页面 */ override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean { if (keyCode == KeyEvent.KEYCODE_BACK && rm_webview.canGoBack()) { rm_webview.goBack() return true } return super.onKeyDown(keyCode, event) } fun goUpPage(keyCode: Int) { object : Thread() { override fun run() { try { val inst = Instrumentation() inst.sendKeyDownUpSync(keyCode) } catch (e: Exception) { e.printStackTrace() } } }.start() }
//调我们的的方法
inner class JsObject { @JavascriptInterface fun openConcat() { //打开通讯录 startNumber(this@H5Activity, 1) } @JavascriptInterface fun finishView() { finish() } }
本文地址:https://blog.csdn.net/weixin_41194750/article/details/107177731
上一篇: vue 模板封装 传递数据
推荐阅读
-
h5是什么,h5软件的优点与缺点
-
h5是什么,h5软件的优点与缺点
-
Apple Watch与Android Wear的交互设计哪个好?UI设计大比拼
-
Android WebView 不支持 H5 input type="file" 解决方法
-
Android开发使用json实现服务器与客户端数据的交互功能示例
-
h5是啥,简述h5技术的优势与应用
-
Apple Watch与Android Wear的交互设计哪个好?UI设计大比拼
-
Android在项目中接入腾讯TBS浏览器WebView的教程与注意的地方
-
详解 WebView 与 JS 交互传值问题
-
iOS中使用JSPatch框架使Objective-C与JavaScript代码交互