Step 1. Add the JitPack repository to your build file
Add it in your root settings.gradle at the end of repositories:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
mavenCentral()
maven { url 'https://jitpack.io' }
}
}
Add it in your settings.gradle.kts at the end of repositories:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
mavenCentral()
maven { url = uri("https://jitpack.io") }
}
}
Add to pom.xml
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
Add it in your build.sbt at the end of resolvers:
resolvers += "jitpack" at "https://jitpack.io"
Add it in your project.clj at the end of repositories:
:repositories [["jitpack" "https://jitpack.io"]]
Step 2. Add the dependency
dependencies {
implementation 'com.github.xuexiangjys:agentweb:1.0.1'
}
dependencies {
implementation("com.github.xuexiangjys:agentweb:1.0.1")
}
<dependency>
<groupId>com.github.xuexiangjys</groupId>
<artifactId>agentweb</artifactId>
<version>1.0.1</version>
</dependency>
libraryDependencies += "com.github.xuexiangjys" % "agentweb" % "1.0.1"
:dependencies [[com.github.xuexiangjys/agentweb "1.0.1"]]
AgentWeb 是一个基于的 Android WebView ,极度容易使用以及功能强大的库,提供了 Android WebView 一系列的问题解决方案 ,并且轻量和极度灵活,体验请下载的 agentweb.apk, 或者你也可以到 Google Play 里面下载 AgentWeb , 详细使用请参照上面的 Sample 。
1.先在项目根目录的 build.gradle 的 repositories 添加:
allprojects {
repositories {
...
maven { url "https://jitpack.io" }
}
}
2.然后在dependencies添加:
dependencies {
...
implementation 'com.github.xuexiangjys.AgentWeb:agentweb-core:1.0.1'(必选)
implementation 'com.github.xuexiangjys.AgentWeb:agentweb-download:1.0.1'(可选)
implementation 'com.github.xuexiangjys.AgentWeb:agentweb-filechooser:1.0.1'(可选)
}
mAgentWeb = AgentWeb.with(this)
.setAgentWebParent((LinearLayout) view, new LinearLayout.LayoutParams(-1, -1))
.useDefaultIndicator()
.createAgentWeb()
.ready()
.go("http://www.jd.com");
<a href="img/img-function-list.png"><img src="img/img-function-list.png" width="30%"/></a> <a href="img/img-permission.png"><img src="img/img-permission.png" width="30%"/></a> <a href="img/img-sonic.png"><img src="img/img-sonic.png" width="30%"/></a>
<a href="img/img-scheme.png"><img src="img/img-scheme.png" width="30%"/></a> <a href="img/img-download.png"><img src="img/img-download.png" width="30%"/></a> <a href="img/img-bounce.png"><img src="img/img-bounce.png" width="30%"/></a>
<a href="img/jd.png"><img src="img/jd.png" width="30%"/></a> <a href="img/wechat pay.png"><img src="img/wechat pay.png" width="30%"/></a> <a href="img/alipay.png"><img src="img/alipay.png" width="30%"/></a>
<a href="img/js.png"><img src="img/js.png" width="30%"/></a> <a href="img/custom setting.png"><img src="img/custom setting.png" width="30%"/></a> <a href="img/video.png"><img src="img/video.png" width="30%"/></a>
function callByAndroid(){
console.log("callByAndroid")
}
mAgentWeb.getJsAccessEntrace().quickCallJs("callByAndroid");
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);
}
@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" />
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();
}
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;
}
};
String cookies=AgentWebConfig.getCookiesByUrl(targetUrl);
AgentWebConfig.syncCookie("http://www.jd.com","ID=XXXX");
//略,请查看 Sample
//略,请查看 Sample
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;
}
};
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());
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.CAMERA"></uses-permission>
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"></uses-permission>
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(该值会更新)
如果你的项目需要加入混淆 , 请加入如下配置
-keep class com.just.agentweb.** {
*;
}
-dontwarn com.just.agentweb.**
Java 注入类不要混淆 , 例如 sample 里面的 AndroidInterface 类 , 需要 Keep 。
-keepclassmembers class com.just.agentweb.sample.common.AndroidInterface{ *; }
AlertDialog
需要依赖 AppCompat
主题 。setAgentWebParent
不支持 ConstraintLayout
。mAgentWeb.getWebLifeCycle().onPause();
会暂停应用内所有WebView
。minSdkVersion
低于等于16以下自定义WebView
请注意与 JS
之间通信安全。 FrameLayout frameLayout = mAgentWeb.getWebCreator().getWebParentLayout();
frameLayout.setBackgroundColor(Color.BLACK);
Sample
(推荐,详细)v_4.0.2 更新
Extra
关闭进度通知setopenbreakPointdownload
命名不规范v_4.0.0 更新
AgentWeb
拆分出 AgentWeb-Download
、 AgentWeb-FileChooser
、AgentWeb-core
三个库,用户可以按需选择AgentWeb-Download
DownloadListener
、DefaultMsgConfig
以及相关APIFragment
和Activity
构建一致。#227BaseAgentWebFragment
和BaseAgentWebActivity
,于Sample形式提供参考WebProgress
进度条动画更细腻FileUpLoadChooserImpl
MiddlewareWebChromeBase
中间件 ,支持多个 WebChromeClient
MiddlewareWebClientBase
中间件 , 支持多个 WebViewClient
AgentWebUIController
,统一控制UI一个位于深圳的 Android 开发者 , 如果你有问题 ,或者工作机会, 请联系 Email : xiaozhongcen@gmail.com
如果你喜欢了 AgentWeb
的设计 , 你也可以请作者喝一杯咖啡。
<a href="img/alipay.jpg"><img src="img/alipay.jpg" width="30%"/></a> <a href="img/wechat_pay.jpg"><img src="img/wechat_pay.jpg" width="30%"/></a> <a href="img/alipay.jpg"><img src="img/alipay.jpg" width="30%"/></a>
Copyright (C) Justson(https://github.com/Justson/AgentWeb)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.