https://github.com/Justson/AgentWeb
build.gradle(Moudle.app)androidx的引用
implementation 'com.just.agentweb:agentweb-androidx:4.1.4'
布局的实现
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
//标题
<include
layout="@layout/activity_left_title_text_view"/>
<LinearLayout
android:id="@+id/view"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
kotlin实现案例
import android.net.http.SslError
import android.os.Bundle
import android.view.KeyEvent
import android.view.View
import android.view.ViewGroup
import android.webkit.SslErrorHandler
import android.webkit.WebView
import com.jeremyliao.liveeventbus.LiveEventBus
import com.just.agentweb.AgentWeb
import com.just.agentweb.WebChromeClient
import kotlinx.android.synthetic.main.activity_left_title_text_view.*
import kotlinx.android.synthetic.main.activity_my_web_view.*
import org.jetbrains.anko.toast
/**
*@Created by wrs on 2020/9/25,11:43
*@Description: h5页面加载
*/
class MyWebActivity : BaseActivity(),View.OnClickListener,JsInterfaceListener {
var mAgentWeb:AgentWeb? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_my_web_view)
initView()
initData()
initListener()
}
override fun initView() {
tvTitle.text = ""
}
private val webChromeClient = object : WebChromeClient(){
override fun onReceivedTitle(view: WebView?, title: String?) {
super.onReceivedTitle(view, title)
tvTitle.text = title
}
}
private fun getWebViewClient(): com.just.agentweb.WebViewClient {
return object : com.just.agentweb.WebViewClient() {
override fun onReceivedSslError(
view: WebView?,
handler: SslErrorHandler,
error: SslError?
) {
handler.proceed()
}
}
}
override fun initData() {
//访问h5的路径
val pathUrl = intent.getStringExtra(MyParms.PARAMS)
mAgentWeb = AgentWeb.with(this)
.setAgentWebParent(view, ViewGroup.LayoutParams(-1, -1))
.useDefaultIndicator()
.setSecurityType(AgentWeb.SecurityType.STRICT_CHECK)
.setWebChromeClient(webChromeClient)
.setWebViewClient(getWebViewClient())
.createAgentWeb()
.ready()
.go(pathUrl)
val webSetting = mAgentWeb?.webCreator?.webView?.settings
webSetting?.javaScriptEnabled = true
//此处为agentweb声明js方法
mAgentWeb?.jsInterfaceHolder?.addJavaObject("android",AndroidInterface(this))
}
override fun initListener() {
ivBack.setOnClickListener(this)
}
override fun jsPullUpMethod(method: String, parmas: String) {
when(method){
"toMain" ->{//js调用android的方法
toast("js调用android的方法")
}
}
}
override fun onClick(v: View?) {
when(v?.id){
R.id.ivBack ->{//返回
if (mAgentWeb?.back() == true){//返回上一层
}else{//关闭当前webview界面
finish()
}
}
}
}
override fun onPause() {
mAgentWeb?.webLifeCycle?.onPause()
super.onPause()
}
override fun onResume() {
mAgentWeb?.webLifeCycle?.onResume()
super.onResume()
}
override fun onDestroy() {
mAgentWeb?.webLifeCycle?.onDestroy()
super.onDestroy()
}
override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
return if (mAgentWeb?.let { it.handleKeyEvent(keyCode, event) } == true) {
true
} else super.onKeyDown(keyCode, event)
}
}
AndroidInterface类
import android.webkit.JavascriptInterface;
/**
*
*
* @Created by wrs on 2020/10/16,13:57
* @Description: js条用android的方法
*/
public class AndroidInterface extends Object {
private JsInterfaceListener jsInterface;
public AndroidInterface(JsInterfaceListener jsInterface) {
this.jsInterface = jsInterface;
}
@JavascriptInterface
public void jsCallMethod(String method,String params){
jsInterface.jsPullUpMethod(method,params);
}
}
js调android的监听方法
package com.dsy.jxih.iml
/**
*@Created by wrs on 2020/10/16,14:00
*@Description: js调用android的方法配合AndroidInterface
*/
interface JsInterfaceListener {
fun jsPullUpMethod(method:String,params:String)
}
js调用android方法的实例
//callAndroid()是android端定义的方法
window.android.callAndroid();
window.android.callAndroid();
记得 android
区分大小写
android调用js方法的实例
mAgentWeb.getJsAccessEntrace().quickCallJs("androidpay");//androidpay()h5里的方法
网友评论