- Notifications
You must be signed in to change notification settings - Fork300
🌏 WebView 全方面使用,JS交互、进度条、上传图片、错误页面、视频全屏播放、唤起原生App、获取网页源代码、被作为第三方浏览器打开、DeepLink、[腾讯x5使用示例]
License
NotificationsYou must be signed in to change notification settings
youlookwhat/ByWebView
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
- 基本配置使用(宽度自适应、返回网页上一层、显示网页标题等)
- 唤起三方应用(拨打电话、发送短信、发送邮件等)
- 上传图片(版本兼容)
- 错误页面处理
- 全屏播放网络视频
- 与Js交互实例
- 优雅的进度条显示控件
Demo示例:
- DeepLink的基本使用
- 被作为第三方浏览器打开
- 与ToolBar联动,自定义WebView
- 腾讯x5使用示例
- Android 关于WebView全方面的使用(项目应用篇)
- Android DeepLink介绍与使用
- Android 应用被作为第三方浏览器打开
- Android WebView与JS交互实例
- 一款Android WebView进度条显示控件,使其加载进度平滑过渡
- Add the JitPack repository to your build file
allprojects {repositories { ...maven {url'https://jitpack.io' } }}
- Add the dependency:
dependencies {implementation'com.github.youlookwhat:ByWebView:1.2.1'}
byWebView =ByWebView .with(this) .setWebParent(container,newLinearLayout.LayoutParams(-1, -1)) .useWebProgress(ContextCompat.getColor(this,R.color.coloRed)) .loadUrl(mUrl);
- 调用Js方法:
// 无参数调用byWebView.getLoadJsHolder().quickCallJs("javacalljs");// 传递参数调用byWebView.getLoadJsHolder().quickCallJs("javacalljswithargs","android传入到网页里的数据,有参");
- Js调用Java方法:
ByWebView.with(this).addJavascriptInterface("injectedObject",newMyJavascriptInterface(this)).loadUrl(mUrl);window.injectedObject.startFunction()
@OverrideprotectedvoidonPause() {super.onPause();byWebView.onPause();}@OverrideprotectedvoidonResume() {super.onResume();byWebView.onResume();}@OverrideprotectedvoidonDestroy() {byWebView.onDestroy();super.onDestroy();}
@OverridepublicbooleanonKeyDown(intkeyCode,KeyEventevent) {if (byWebView.handleKeyEvent(keyCode,event)) {returntrue; }else {returnsuper.onKeyDown(keyCode,event); }}
@OverrideprotectedvoidonActivityResult(intrequestCode,intresultCode,Intentintent) {byWebView.handleFileChooser(requestCode,resultCode,intent);}
byWebView =ByWebView .with(this) .setWebParent(container,newLinearLayout.LayoutParams(-1, -1)) .useWebProgress(ContextCompat.getColor(this,R.color.coloRed)) .setOnTitleProgressCallback(onTitleProgressCallback) .setOnByWebClientCallback(onByWebClientCallback) .addJavascriptInterface("injectedObject",newMyJavascriptInterface(this)) .loadUrl(mUrl);
privateOnTitleProgressCallbackonTitleProgressCallback =newOnTitleProgressCallback() {@OverridepublicvoidonReceivedTitle(Stringtitle) {Log.e("---title",title); }@OverridepublicvoidonProgressChanged(intnewProgress) {// 返回的进度 }@OverridepublicbooleanonHandleScreenOrientation(booleanisShow) {// 返回true,视频全屏时,横竖屏自己处理returnfalse; }};privateOnByWebClientCallbackonByWebClientCallback =newOnByWebClientCallback() {@OverridepublicvoidonPageStarted(WebViewview,Stringurl,Bitmapfavicon) { }@OverridepublicbooleanonReceivedSslError(WebViewview,SslErrorHandlerhandler,SslErrorerror) {// 如果自己处理,需要返回truereturnsuper.onReceivedSslError(view,handler,error); }@OverridepublicvoidonPageFinished(WebViewview,Stringurl) {// 网页加载完成后的回调 }@OverridepublicbooleanisOpenThirdApp(Stringurl) {// 处理三方链接Log.e("---url",url);returnByWebTools.handleThirdApp(ByWebViewActivity.this,url); }};
ByWebView.with(this).setErrorLayout(R.layout.by_load_url_error,"无法打开网页").loadUrl(mUrl);
byWebView =ByWebView .with(this) .useWebProgress(true)// 是否使用进度条,默认true,如使用可不用配置 .setWebParent(container,newLinearLayout.LayoutParams(-1, -1))// 设置WebView父容器 .useWebProgress("#ffb6cf","#ff0000",3)// 进度条渐变色(开始颜色,结束颜色,高度) .setOnTitleProgressCallback(onTitleProgressCallback)// title 和 progress 监听 .setOnByWebClientCallback(onByWebClientCallback)// WebViewClient监听 .setErrorLayout(R.layout.by_load_url_error,"无法打开网页")// 设置错误页面及标题 .addJavascriptInterface("injectedObject",newMyJavascriptInterface(this))// 设置Js监听 .loadUrl(mUrl);
混淆时应加上(通过JS向网页传值,如不加有时候会传值失败):
-keepattributes *Annotation*-keepattributes *JavascriptInterface*-keepclassmembersclass * {@android.webkit.JavascriptInterface <methods>;}
About
🌏 WebView 全方面使用,JS交互、进度条、上传图片、错误页面、视频全屏播放、唤起原生App、获取网页源代码、被作为第三方浏览器打开、DeepLink、[腾讯x5使用示例]