项目地址:https://link.jianshu.com/?t=https://github.com/Justson/AgentWeb
AgentWeb 是一个基于的 Android WebView ,极度容易使用以及功能强大的库,提供了 Android WebView 一系列的问题解决方案 ,并且轻量和极度灵活
引入
Gradle
api ‘com.just.agentweb:agentweb:4.0.3-beta’ // (必选)
api ‘com.just.agentweb:filechooser:4.0.3-beta’// (可选)
api ‘com.just.agentweb:download:4.0.3-beta’ // (可选)
api ‘com.github.Justson:Downloader:v4.0.3’// (可选)
基础用法
mAgentWeb = AgentWeb.with(this)
.setAgentWebParent(view, new LinearLayout.LayoutParams(-1, -1))
.useDefaultIndicator()
.createAgentWeb()
.ready()
.go("URL");
调用 Javascript 方法拼接太麻烦 ? 请看 。
function callByAndroid(){
console.log("callByAndroid")
}
mAgentWeb.getJsAccessEntrace().quickCallJs("callByAndroid");
Javascript 调 Java ?
mAgentWeb.getJsInterfaceHolder().addJavaObject("android",new AndroidInterface(mAgentWeb,this));
window.android.callAndroid();
事件处理
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
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 文件里面加入如下权限
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
}
};
返回上一页
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;
}
};
查看 Cookies
String cookies=AgentWebConfig.getCookiesByUrl(targetUrl);
同步 Cookie
AgentWebConfig.syncCookie("http://www.jd.com","ID=XXXX");
MiddlewareWebChromeBase 支持多个 WebChromeClient
清空缓存
AgentWebConfig.clearDiskCache(this.getContext());
权限拦截
protected PermissionInterceptor mPermissionInterceptor = new PermissionInterceptor() {
@Override
public boolean intercept(String url, String[] permissions, String action) {
Log.i(TAG, "url:" + url + " permission:" + permissions + " action:" + action);
return false;
}
};
AgentWeb 完整用法
mAgentWeb = AgentWeb.with(this)
.setAgentWebParent((LinearLayout) view, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT))
.useDefaultIndicator(-1, 3)
.setAgentWebWebSettings(getSettings())
.setWebViewClient(mWebViewClient)
.setWebChromeClient(mWebChromeClient)
.setPermissionInterceptor(mPermissionInterceptor)
.setSecurityType(AgentWeb.SecurityType.STRICT_CHECK)
.setAgentWebUIController(new UIController(getActivity()))
.setMainFrameErrorView(R.layout.agentweb_error_page, -1)
.useMiddlewareWebChrome(getMiddlewareWebChrome())
.useMiddlewareWebClient(getMiddlewareWebClient())
.setOpenOtherPageWays(DefaultWebClient.OpenOtherPageWays.DISALLOW)
.interceptUnkownUrl()
.createAgentWeb()
.ready()
.go(getUrl());
AgentWeb 所需要的权限(在你工程中根据需求选择加入权限)
AgentWeb 所依赖的库
compile "com.android.support:design:${SUPPORT_LIB_VERSION}" // (3.0.0开始该库可选)
compile "com.android.support:support-v4:${SUPPORT_LIB_VERSION}"
SUPPORT_LIB_VERSION=27.0.2(该值会更新)