https://github.com/Justson/AgentWeb
build.gradle(Moudle.app)androidx的引用
implementation 'com.just.agentweb:agentweb-androidx:4.1.4'
如果項(xiàng)目中h5需要選擇圖片或者是下載需要添加一下依賴
implementation 'com.just.agentweb:filechooser:4.1.4'// (可選)
implementation 'com.download.library:Downloader:4.1.4'// (可選)
如果添加選擇圖片或下載報(bào)錯(cuò),需要添加一下配置build.gradle(Project:項(xiàng)目名稱)
allprojects {
repositories {
...
maven { url "https://jitpack.io" }
}
}
布局的實(shí)現(xiàn)
<?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">
//標(biāo)題
<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實(shí)現(xiàn)案例
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調(diào)用android的方法
toast("js調(diào)用android的方法")
}
"toPay" ->{//js調(diào)用android的方法
toast("js調(diào)用android的方法")
}
}
}
override fun onClick(v: View?) {
when(v?.id){
R.id.ivBack ->{//返回
if (mAgentWeb?.back() == true){//返回上一層
}else{//關(guān)閉當(dāng)前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調(diào)android的監(jiān)聽方法
/**
*@Created by wrs on 2020/10/16,14:00
*@Description: js調(diào)用android的方法配合AndroidInterface
*/
interface JsInterfaceListener {
fun jsPullUpMethod(method:String,params:String)
}
js調(diào)用android方法的實(shí)例
//toMain()是android端定義的方法 不傳參數(shù)
window.android.toMain();
//toPay("支付參數(shù)")是android端定義的方法 傳參數(shù)
window.android.toPay("支付參數(shù)");
window.android.toMain();記得 android區(qū)分大小寫
android調(diào)用js方法的實(shí)例
//androidpay()h5里的方法 不傳參數(shù)
mAgentWeb.getJsAccessEntrace().quickCallJs("androidpay");
//androidpay()h5里的方法 傳參數(shù)
mAgentWeb.getJsAccessEntrace().quickCallJs("androidShare","分享的數(shù)據(jù)");
mAgentWeb.getJsAccessEntrace().quickCallJs("androidShare","參數(shù)1","參數(shù)2","參數(shù)3");
<script >
export default{
data(){
return{
}
},
onLoad() {
},
mounted() {
//這里必須要掛載
window.androidpay= this.androidpay
window.androidShare= this.androidShare
},
methods:{
androidpay(){
console.log("調(diào)用到了androidpay");
},
androidShare(data){
console.log("調(diào)用到了androidShare");
},
}
}
</script>
agentweb加載其它h5界面
runOnUiThread {
mAgentWeb?.webCreator?.webView?.loadUrl("url地址")
}
agentweb刷新
mAgentWeb?.webCreator?.webView?.reload()
如果前段那邊需要調(diào)試debug可以配置一下設(shè)置
AgentWebConfig.debug();
清空歷史記錄
AgentWebConfig.clearDiskCache(this.getContext());
AgentWeb加載html的url
//val path = "<html><head><meta http-equiv='Content-Type' content='text/html; charset=UTF-8'><title></title></head><body><script type='text/javascript'>location.
mAgentWeb?.webCreator?.webView?.loadDataWithBaseURL(null,path,"text/html","utf-8",null)