
圖片來(lái)源網(wǎng)絡(luò),入侵必刪
在維護(hù)一些項(xiàng)目的時(shí)候,你可以會(huì)見(jiàn)到H5頁(yè)面是使用AgentWeb來(lái)加載的。這篇博客來(lái)分享一下AgentWeb相關(guān)的知識(shí),希望對(duì)你有所幫助。
AgentWeb
AgentWeb是一個(gè)基于的 Android WebView ,極度容易使用以及功能強(qiáng)大的庫(kù),提供了 Android WebView 一系列的問(wèn)題解決方案 ,并且輕量和極度靈活。
特性
- 支持進(jìn)度條以及自定義進(jìn)度條
- 支持文件下載
- 支持文件下載斷點(diǎn)續(xù)傳
- 支持下載通知形式提示進(jìn)度
- 簡(jiǎn)化 Javascript 通信
- 支持 Android 4.4 Kitkat 以及其他版本文件上傳
- 支持注入 Cookies
- 加強(qiáng) Web 安全
- 支持全屏播放視頻
- 兼容低版本 Js 安全通信
- 更省電 。
- 支持調(diào)起微信支付
- 支持調(diào)起支付寶(請(qǐng)參照sample)
- 默認(rèn)支持定位
- 支持傳入 WebLayout(下拉回彈效果)
- 支持自定義 WebView
- 支持 JsBridge。
導(dǎo)入項(xiàng)目
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
非AndroidX項(xiàng)目:
implementation 'com.github.Justson.AgentWeb:agentweb-core:v5.0.0-alpha' // (必選)
implementation 'com.github.Justson.AgentWeb:agentweb-filechooser:v5.0.0-alpha' // (可選)
implementation 'com.github.Justson:Downloader:v5.0.0' // (可選)
AndroidX項(xiàng)目:
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' // (可選)
我項(xiàng)目使用的是5.0.0版本,如果想看有沒(méi)有更新可以看開(kāi)源庫(kù)的wiki
使用示例
基礎(chǔ)用法:
mAgentWeb = AgentWeb.with(this)
.setAgentWebParent((LinearLayout) view, new LinearLayout.LayoutParams(-1, -1))
.useDefaultIndicator()
.createAgentWeb()
.ready()
.go("http://www.jd.com");
事件處理:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (mAgentWeb.handleKeyEvent(keyCode, event)) {
return true;
}
return super.onKeyDown(keyCode, event);
}
WebChromeClient 與 WebViewClient:
AgentWeb.with(this)
.setAgentWebParent(mLinearLayout,new LinearLayout.LayoutParams(-1,-1) )
.useDefaultIndicator()
.setReceivedTitleCallback(mCallback)
.setWebChromeClient(mWebChromeClient)
.setWebViewClient(mWebViewClient)
.setSecutityType(AgentWeb.SecurityType.strict)
.createAgentWeb()
.ready()
.go(getUrl());
private WebViewClient mWebViewClient=new WebViewClient(){
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
//do you work
}
};
private WebChromeClient mWebChromeClient=new WebChromeClient(){
@Override
public void onProgressChanged(WebView view, int newProgress) {
//do you work
}
};
清空緩存(這是很多App都會(huì)用到的):
AgentWebConfig.clearDiskCache(this.getContext());
混淆代碼
-keep class com.just.agentweb.** {
*;
}
-dontwarn com.just.agentweb.**
注意事項(xiàng)
開(kāi)源的大佬提示需要注意以下情況:
- 支付寶使用需要引入支付寶SDK ,并在項(xiàng)目中依賴 , 微信支付不需要做任何操作。
-
AgentWeb內(nèi)部使用了AlertDialog需要依賴AppCompat主題 。 -
setAgentWebParent不支持ConstraintLayout。 -
mAgentWeb.getWebLifeCycle().onPause();會(huì)暫停應(yīng)用內(nèi)所有WebView。 -
minSdkVersion低于等于16以下自定義WebView請(qǐng)注意與JS之間通信安全。