Android 打开网页之CustomTabs
一般打开url有以下几种方式:
- 调用默认浏览器
- 用WebView
- 用CustomTabsIntent
默认浏览器的话,是跳转到别的app,我方app就切换到后台了,至于什么时候返回回来就不确定了,且在后台有被回收的风险,不利于业务开展,如果是打开浏览器之后就完成任务的情况,可以使用。
WebView是我们比较常用的,如果页面可控,且需要交互的,还是建议使用webview的。
CustomTabs在用户的默认浏览器中显示网页,相当于在自己的app中用默认浏览器打开网页,效果类似WebView,但使用起来比WebView轻量,也更安全,性能更好。
今天主要说说CustomTabs。
效果:
这是打开应用宝的一个App下载链接。可以看到加载速度还是很快的,就像打开activity一样。
引入
customtabs其实是browser包下的,browser是jetpack下的,现在统一纳入到androidx。
dependencies {implementation "androidx.browser:browser:1.3.0"
}
使用
简单使用的话,只要一行代码
CustomTabsIntent.Builder().build().launchUrl(context, uri)
就这么简单。
上面提到效果图中打开网页像打开activity一样,如果更像呢,主题一直是不是更像了。
定制ui
CustomTabsIntent也支持定制ui
//设置颜色方案
val schemeParams = CustomTabColorSchemeParams.Builder().setToolbarColor(ContextCompat.getColor(context, R.color.colorPrimary)).setSecondaryToolbarColor(ContextCompat.getColor(context, R.color.colorPrimaryDark)).build()CustomTabsIntent.Builder().setDefaultColorSchemeParams(schemeParams).build().launchUrl(context, uri)
CustomTabColorSchemeParams支持
- toolbarColor
- secondaryToolbarColor
- navigationBarColor
- navigationBarDividerColor
看看效果
菜单
比如setActionButton
val bitmap = BitmapFactory.decodeResource(this.resources, R.mipmap.ic_setting)
val intent = Intent(context, LoginActivity::class.java)
val activity = PendingIntent.getActivity(context, 0, intent, 0)
//内置启动
CustomTabsIntent.Builder().setActionButton(bitmap, "自定义Action", activity).setDefaultColorSchemeParams(schemeParams).build().launchUrl(context, uri)
这里用PendingIntent指定后续操作,比如打开一个页面或者发送一个广播。
可以看到右上角多了一个设置的小图标。
除此之外还有很多别的api,比如:
- addMenuItem(String label, PendingIntent pendingIntent)
- setCloseButtonIcon(Bitmap icon)
- setShowTitle(boolean showTitle)
- 等等
完整代码
btn_launch.setOnClickListener {openWebPage(this, Uri.parse("https://www.baidu.com"))}...private fun openWebPage(context: Context, uri: Uri) {if (context.isChromeSupported()) {//设置颜色方案val schemeParams = CustomTabColorSchemeParams.Builder().setToolbarColor(ContextCompat.getColor(context, R.color.colorPrimary)).setSecondaryToolbarColor(ContextCompat.getColor(context, R.color.colorPrimaryDark)).build()val bitmap = BitmapFactory.decodeResource(this.resources, R.mipmap.ic_setting)val intent = Intent(context, LoginActivity::class.java)val activity = PendingIntent.getActivity(context, 0, intent, 0)//内置启动CustomTabsIntent.Builder().setActionButton(bitmap, "自定义Action", activity).setDefaultColorSchemeParams(schemeParams).build().launchUrl(context, uri)} else {//启动默认浏览器context.startActivity(Intent(Intent.ACTION_VIEW, uri))}}private fun Context.isChromeSupported(): Boolean {val serviceIntent = Intent(CustomTabsService.ACTION_CUSTOM_TABS_CONNECTION)serviceIntent.setPackage("com.android.chrome")val resolveInfos = packageManager.queryIntentServices(serviceIntent, 0)return !resolveInfos.isNullOrEmpty()}
感谢
- 官方文档
- 提升体验-支持Chrome Custom Tabs
Android 打开网页之CustomTabs相关推荐
- android 打开网页的两种方式.
有时候我们要在自己的应用中打开一个网页. 这是本人用到 的两种方式. 1. 网页放置于自己的ACTIVITY 1.1 在资源文件中申明WIDGET <WebView ...
- Android 打开网页搜索关键词
String searchStr = "eoe"; /* 取得网页搜寻的intent */ Intent search = new Intent(Intent.ACTION_WEB ...
- android自动弹出浏览器打开文件,android使用主流浏览器打开网页,无需弹出选择。...
首先说一下开发需求,需要使用主流的浏览器打开网页,比如QQ浏览器,UC浏览器,opera浏览器,如果此手机里面有安装上述浏览器,则使用上述的浏览器打开网页,无需弹出浏览器选择项,如果这几种都有安装则按 ...
- Android 通过网页打开自己的APP(scheme)
Android 通过网页打开自己的APP(scheme) 分类: android2014-07-09 17:35 8565人阅读 评论(2) 收藏 举报 通过用手机的浏览器(内置,第三方都可)访问一个 ...
- android仿微信图片上传进度,Android开发之模仿微信打开网页的进度条效果(高仿)...
一,为什么说是真正的高仿? 阐述这个问题前,先说下之前网上的,各位可以复制这段字,去百度一下 "仿微信打开网页的进度条效果",你会看到有很多类似的文章,不过他们有个共同点,就是实 ...
- android安装自动打开网页,Android调用系统自带浏览器打开网页的实现方法
Android调用系统自带浏览器打开网页的实现方法 在Android中可以调用自带的浏览器,或者指定一个浏览器来打开一个链接.只需要传入一个uri,可以是链接地址. 启动android默认浏览器 在A ...
- android仿微信 进度条,Android开发之模仿微信打开网页的进度条效果(高仿)
一,为什么说是真正的高仿? 阐述这个问题前,先说下之前网上的,各位可以复制这段字,去百度一下 "仿微信打开网页的进度条效果" ,你会看到有很多类似的文章,不过他们有个共同点,就是 ...
- Android Studio调用谷歌浏览器打开网页
前提是我们android手机上有谷歌浏览器,默认使用谷歌打开指定链接,若失败则调用android本地默认浏览器打开网页,代码如下: Uri uri = Uri.parse("https:// ...
- Android获取本机浏览器包名,并调用指定浏览器打开网页
昨天还在嘀咕着项目快做完了,接下来公司会安排什么事,结果今天就给了我一个新任务,其中要做的一个功能就是指定调用谷歌浏览器打开网页,如果没有该浏览器就提示客户进行下载,查找一番资料后,觉得这种东西还是要 ...
最新文章
- SpringBoot+ElementUI实现通用文件下载请求(全流程图文详细教程)
- C的function call與stack frame心得
- Android传感器编程入门
- junit5 动态测试_JUnit 5 –动态测试
- 数据存储和界面展示(二)
- 64位汇编中的布尔指令
- 能用计算机解决的问题十个,计算机常见问题及解决方法,计算机十项常见故障...
- deque iterator not dereferencable 问题
- java反编译工具jad和jd-gui使用
- Python爬虫你需要积累这些基本知识_Python学习基础路线
- cad怎样弄出放线的坐标_不知道如何定位放线?今天教大家一个快速准确放线方法(含实例)...
- 常见的编程语言的特点与应用领域浅谈
- python教程系列(三.6.2、XML)
- vue姓名动态输入三种方式、前端密码加密
- 注塑缺陷浮纤的原因及解决对策
- 使用Kotlin语言两年后,我有话要说
- 微信小程序地图图标controltap
- PNP NPN 三线制接近开关接线
- 微信php开发包,TP5专用微信sdk使用教程
- 夏季必不能少六道凉拌菜