AgentWeb 是一个基于 Android 的 WebView 功能强大的组件库。在 WebView 性能和用户体验做了很多的优化,目前有网友开源在了 github 上。本文主要负责讲解它的一些简单的用法。
AgentWeb 介绍
AgentWeb 是一个基于的 Android WebView ,极度容易使用以及功能强大的库 。

AgentWeb 功能
- 支持进度条以及自定义进度条
- 支持文件下载
- 支持文件下载断点续传
- 支持下载通知形式提示进度
- 简化 Javascript 通信
- 支持 Android 4.4 Kitkat 以及其他版本文件上传
- 支持注入 Cookies
- 加强 Web 安全
- 兼容低版本安全 Js 通信
AgentWeb 的优点
| Web | 文件下载 | 文件上传 | Js 通信 | 断点续传 | 使用简易度 | 进度条 | 线程安全 |
|---|---|---|---|---|---|---|---|
| WebView | 不支持 | 不支持 | 支持 | 不支持 | 麻烦 | 没有 | 不安全 |
| AgentWeb | 支持 | 支持 | 更简洁 | 支持 | 简洁 | 有 | 安全 |
基本用法
在使用 之前,我们先引入它。
-
Gradle
compile 'com.just.agentweb:agentweb:4.0.0' // (必选) compile 'com.just.agentweb:download:4.0.0' // (可选) compile 'com.just.agentweb:filechooser:4.0.0'// (可选)
-
Maven
<dependency> <groupId>com.just.agentweb</groupId> <artifactId>agentweb</artifactId> <version>4.0.0</version> <type>pom</type> </dependency>
看下面的例子:
mAgentWeb = AgentWeb.with(this)
.setAgentWebParent((LinearLayout) view, new LinearLayout.LayoutParams(-1, -1))
.useDefaultIndicator()
.createAgentWeb()
.ready()
.go("http://www.xttblog.com");
运行效果如下所示:

调用 Javascript 方法
function callByAndroid(){
console.log("callByAndroid")
}
mAgentWeb.getJsEntraceAccess().quickCallJs("callByAndroid");
Javascript 调 Java
mAgentWeb.getJsInterfaceHolder().addJavaObject("android",new AndroidInterface(mAgentWeb,this));
window.android.callAndroid();
事件处理
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// :www.xttblog.com
if (mAgentWeb.handleKeyEvent(keyCode, event)) {
return true;
}
return super.onKeyDown(keyCode, event);
}
跟随 Activity Or Fragment 生命周期 , 释放 CPU 更省电 。
@Override
protected void onPause() {
mAgentWeb.getWebLifeCycle().onPause();
super.onPause();
}
@Override
protected void onResume() {
mAgentWeb.getWebLifeCycle().onResume();
super.onResume();
}
@Override
public void onDestroyView() {
mAgentWeb.getWebLifeCycle().onDestroy();
super.onDestroyView();
}
全屏视频播放
<!--如果你的应用需要用到视频 , 那么请你在使用 AgentWeb 的 Activity 对应的清单文件里加入如下配置--> android:hardwareAccelerated="true" android:configChanges="orientation|screenSize"
定位
<!--AgentWeb 是默认允许定位的 ,如果你需要该功能 , 请在你的 AndroidManifest 文件里面加入如下权限 。--> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
WebChromeClient Or 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
}
};
返回上一页
if (!mAgentWeb.back()){
AgentWebFragment.this.getActivity().finish();
}
获取 WebView
mAgentWeb.getWebCreator().getWebView();
文件下载监听
protected DownloadListenerAdapter mDownloadListenerAdapter = new DownloadListenerAdapter() {
@Override
public boolean onStart(String url, String userAgent, String contentDisposition, String mimetype,
long contentLength, AgentWebDownloader.Extra extra) {
extra.setOpenBreakPointDownload(true)
.setIcon(R.drawable.ic_file_download_black_24dp)
.setConnectTimeOut(6000)
.setBlockMaxTime(2000)
.setDownloadTimeOut(60L * 5L * 1000L)
.setAutoOpen(true)
.setForceDownload(false);
return false;
}
@Override
public void onBindService(String url, DownloadingService downloadingService) {
super.onBindService(url, downloadingService);
mDownloadingService = downloadingService;
LogUtils.i(TAG, "onBindService:" + url + " DownloadingService:" + downloadingService);
}
@Override
public void onUnbindService(String url, DownloadingService downloadingService) {
super.onUnbindService(url, downloadingService);
mDownloadingService = null;
LogUtils.i(TAG, "onUnbindService:" + url);
}
@Override
public void onProgress(String url, long loaded, long length, long usedTime) {
int mProgress = (int) ((loaded) / Float.valueOf(length) * 100);
LogUtils.i(TAG, "onProgress:" + mProgress);
super.onProgress(url, loaded, length, usedTime);
}
@Override
public boolean onResult(String path, String url, Throwable throwable) {
if (null == throwable) {
//do you work
} else {
}
return false;
}
};
AgentWeb 的功能很强大,还有很多实用的功能,请参考官网 api 和相关 demo。
AgentWeb 内部结构

- IndicatorController 进度条控制器
- WebCreator 创建 WebView 。
- WebSettings 统一设置 WebView 的 settings
- WebSecurityController 安全控制器
- JsEntraceAccess Js 方法入口

: » AgentWeb 教程
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/251710.html