WebView与H5交互

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()
    }


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值