gddhy

_(:з」∠)_ 加载中...
  • 主页
  • 归档
  • 工具
  • 关于
所有文章 友链

gddhy

_(:з」∠)_ 加载中...

  • 主页
  • 归档
  • 工具
  • 关于

ChromeCustomTabs快速使用

2023-03-03
字数统计:372字 阅读时长≈1分

CustomTabs在用户的默认浏览器中显示网页,相当于在自己的app中用默认浏览器打开网页,效果类似WebView,但使用起来比WebView轻量,也更安全,性能更好。

使用CustomTabs可以使用浏览器中最新的内核,避免系统SystemWebView版本过低导致部分功能不支持

新版Edge已经换到Chromium内核,也支持CustomTabs功能

  • Edge 108内核最低兼容安卓6
  • Edge 110内核最低兼容安卓7

豌豆荚Edge下载

快速使用:

引入依赖

1
implementation "androidx.browser:browser:1.4.0"

安卓11以上系统检测时要适配软件包可见性

检测Chrome是否支持CustomTabs

1
2
3
4
5
6
7
8
9
10
11
12
private static boolean isChromeSupported(Context context){
PackageManager pm = context.getPackageManager();
Intent serviceIntent = new Intent(CustomTabsService.ACTION_CUSTOM_TABS_CONNECTION);
serviceIntent.setPackage("com.android.chrome");

List<ResolveInfo> resolveInfo = pm.queryIntentServices(serviceIntent, 0);
if(resolveInfo == null){
return false;
}

return !resolveInfo.isEmpty();
}

检测Edge是否支持CustomTabs

1
2
3
4
5
6
7
8
9
10
11
12
private static boolean isEdgeSupported(Context context){
PackageManager pm = context.getPackageManager();
Intent serviceIntent = new Intent(CustomTabsService.ACTION_CUSTOM_TABS_CONNECTION);
serviceIntent.setPackage("com.microsoft.emmx");

List<ResolveInfo> resolveInfo = pm.queryIntentServices(serviceIntent, 0);
if(resolveInfo == null){
return false;
}

return !resolveInfo.isEmpty();
}

调用CustomTabs打开网页

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public static boolean openChromeCustomTab(Context context,String url){
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
builder.setShowTitle(true);
CustomTabsIntent customTabsIntent = builder.build();
if(isEdgeSupported(context)){
customTabsIntent.intent.setPackage("com.microsoft.emmx");
} else if (isChromeSupported(context)) {
customTabsIntent.intent.setPackage("com.android.chrome");
} else {
return false;
}
customTabsIntent.launchUrl(context, Uri.parse(url));
return true;
}

当手机中没有浏览器支持CustomTabs,使用customTabsIntent.launchUrl()会跳转到默认浏览器或者打开浏览器选择

参考:Android 打开网页之CustomTabs

赏

谢谢你请我吃糖果

微信

扫一扫,分享到微信

微信分享二维码
山东电信资费公示
博客域名
  1. 1. 快速使用:
留言已关闭
© gddhy
Hexo Theme Yilia by Litten
  • 所有文章
  • 友链

tag:

  • 软件分享
  • Android
  • Hexo
  • game
  • Html
  • Java
  • mtk
  • MIUI
  • 旧机博物馆
  • git
  • Termux
  • 原神
  • Win
  • 安卓学习笔记

    缺失模块

  • Luminous' Home
  • 影子博客
  • 四次元领域
  • 初之音
  • Mr.Pumpkin
  • JUSEBLOG
  • ZhaoQuinn 's Blog