Chrome Custom Tabs

官方链接:https://developer.chrome.com/multidevice/android/customtabs

image.png

1.

根据官方描述,Chrome Custom Tabs的速度比WebView快2倍左右,Chrome Custom Tabs其实是使用了默认浏览器来打开页面。相对于WebView而言,Chrome Custom Tabs无法通过js回调本地方法。所以一般来说如果要加载的网页是我们自己的(自己公司前端写的)就用WebView,如果加载的是外部网页,比如一个外部新闻链接,那么就用Chrome Custom Tabs

2.

一般来说在加载网页之前是需要判断的(从用户体验上来说,是用WebView还是Chrome Custom Tabs),即使要加载的是外部网页,因为默认的浏览器可能不是原生的。比如说用户下载了qq浏览器并将qq浏览器设置为了默认浏览器,这时候你再用Chrome Custom Tabs来打开会发现它启动了qq浏览器,很明显的启动效果(跟之前使用webView时不在app内加载是一样的效果,体验很差)

3.

判断
官方demo:https://github.com/GoogleChrome/custom-tabs-client
demo中的判断方法是在CustomTabsHelper类中

 /**
     * Goes through all apps that handle VIEW intents and have a warmup service. Picks
     * the one chosen by the user if there is one, otherwise makes a best effort to return a
     * valid package name.
     *
     * This is <strong>not</strong> threadsafe.
     *
     * @param context {@link Context} to use for accessing {@link PackageManager}.
     * @return The package name recommended to use for connecting to custom tabs related components.
     */
    public static String getPackageNameToUse(Context context) {
        if (sPackageNameToUse != null) return sPackageNameToUse;

        PackageManager pm = context.getPackageManager();
        // Get default VIEW intent handler.
        Intent activityIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.example.com"));
        ResolveInfo defaultViewHandlerInfo = pm.resolveActivity(activityIntent, 0);
        String defaultViewHandlerPackageName = null;
        if (defaultViewHandlerInfo != null) {
            defaultViewHandlerPackageName = defaultViewHandlerInfo.activityInfo.packageName;
        }

        // Get all apps that can handle VIEW intents.
        List<ResolveInfo> resolvedActivityList = pm.queryIntentActivities(activityIntent, 0);
        List<String> packagesSupportingCustomTabs = new ArrayList<>();
        for (ResolveInfo info : resolvedActivityList) {
            Intent serviceIntent = new Intent();
            serviceIntent.setAction(ACTION_CUSTOM_TABS_CONNECTION);
            serviceIntent.setPackage(info.activityInfo.packageName);
            if (pm.resolveService(serviceIntent, 0) != null) {
                packagesSupportingCustomTabs.add(info.activityInfo.packageName);
            }
        }

        // Now packagesSupportingCustomTabs contains all apps that can handle both VIEW intents
        // and service calls.
        if (packagesSupportingCustomTabs.isEmpty()) {
            sPackageNameToUse = null;
        } else if (packagesSupportingCustomTabs.size() == 1) {
            sPackageNameToUse = packagesSupportingCustomTabs.get(0);
        } else if (!TextUtils.isEmpty(defaultViewHandlerPackageName)
                && !hasSpecializedHandlerIntents(context, activityIntent)
                && packagesSupportingCustomTabs.contains(defaultViewHandlerPackageName)) {
            sPackageNameToUse = defaultViewHandlerPackageName;
        } else if (packagesSupportingCustomTabs.contains(STABLE_PACKAGE)) {
            sPackageNameToUse = STABLE_PACKAGE;
        } else if (packagesSupportingCustomTabs.contains(BETA_PACKAGE)) {
            sPackageNameToUse = BETA_PACKAGE;
        } else if (packagesSupportingCustomTabs.contains(DEV_PACKAGE)) {
            sPackageNameToUse = DEV_PACKAGE;
        } else if (packagesSupportingCustomTabs.contains(LOCAL_PACKAGE)) {
            sPackageNameToUse = LOCAL_PACKAGE;
        }
        return sPackageNameToUse;
    }

如果把qq浏览器设置为默认,那么这个会返回null。

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 原文章地址:Android 开发小工具之:Custom Tabs 上周 Android Support 库偷偷摸摸...
    如若成枫阅读 2,498评论 0 0
  • chrome扩展开发入门教程 最近在开发chrome插件,看到一篇非常适合入门的教程,特记录一下 注:转载 本文首...
    谢大见阅读 6,537评论 1 25
  • 这篇博客主要来介绍 WebView 的相关使用方法,常见的几个漏洞,开发中可能遇到的坑和最后解决相应漏洞的源码,以...
    Shawn_Dut阅读 7,509评论 3 55
  • 我今天去龙港外滩玩。 我首先来到了龙翔广场。广场足足有有个足球场那么大,地面铺着许多的小方砖,铺成一个光滑的地...
    0502ab119bf2阅读 492评论 0 0
  • 什么是涌现呢? 涌现来自对“复杂系统”的研究,是复杂系统中最显著也是最重要的一种特征。 在系统科学中它说的是,大量...
    心境映花容阅读 1,586评论 1 2

友情链接更多精彩内容