AgentWeb 是一个基于的 Android WebView ,极度容易使用以及功能强大的库,提供了 Android WebView 一系列的问题解决方案 ,并且轻量和极度灵活。
使用 引用依赖 implementation 'com.github.Justson.AgentWeb:agentweb-core:v5.0.0-alpha.1-androidx' // (必选) implementation 'com.github.Justson.AgentWeb:agentweb-filechooser:v5.0.0-alpha.1-androidx' // (可选) implementation 'com.github.Justson:Downloader:v5.0.0-androidx' // (可选) 布局文件 <?xml version="1.0" encoding="utf-8"?><layout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools"> <data> </data> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" tools:context=".ui.activity.MainActivity"> <androidx.appcompat.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"> <TextView android:id="@+id/tvTitle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:ellipsize="end" android:lines="1" android:textColor="@color/black" android:textSize="16sp" tools:text="@string/app_name" /> </androidx.appcompat.widget.Toolbar> <androidx.appcompat.widget.LinearLayoutCompat android:id="@+id/container" android:layout_width="match_parent" android:layout_height="0dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/toolbar" /> </androidx.constraintlayout.widget.ConstraintLayout></layout> 初始化 val mAgentWeb = AgentWeb.with(this) .setAgentWebParent(binding.container, -1, LinearLayoutCompat.LayoutParams(-1, -1)) .useDefaultIndicator() .setWebChromeClient(mChromeChromeClient) .setWebViewClient(mWebViewClient) .setOpenOtherPageWays(DefaultWebClient.OpenOtherPageWays.ASK) .createAgentWeb() .ready() .get() mAgentWeb.urlLoader.loadUrl(URL)或者直接访问网页
val mAgentWeb = AgentWeb.with(this) .setAgentWebParent(binding.container, -1, LinearLayoutCompat.LayoutParams(-1, -1)) .useDefaultIndicator() .setWebChromeClient(mChromeChromeClient) .setWebViewClient(mWebViewClient) .setOpenOtherPageWays(DefaultWebClient.OpenOtherPageWays.ASK) .createAgentWeb() .go(HOME_URL) webChromeClinet设置 private val mChromeChromeClient = object : WebChromeClient() { override fun onReceivedTitle(view: WebView?, title: String?) { binding.tvTitle.text = title } } Javascript 通信//Javascript 方法function callByAndroid(){ console.log("callByAndroid")}
Android 端
mAgentWeb.getJsEntraceAccess().quickCallJs("callByAndroid");