如何检查是否铬镀铬支持自定义选项卡? [英] How can I check whether Chrome supports Chrome custom tabs?

查看:179
本文介绍了如何检查是否铬镀铬支持自定义选项卡?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个加载外部链接到我的应用程序中的web视图的活动。我想使用Chrome自定义选项卡时,它的可用,但我支持可能不会有一个版本的Chrome浏览器,支持他们的设备。

在不被支持的,我想用我的旧的code,但使用CustomTabsIntent.Builder()的时候都CustomTabs的情况。旧code加载包含在一个活动,我仍然可以管理的ActionBar一个web视图的内容。

我想写一个辅助方法,将告诉我,如果它的支持,但我不知道怎么样。开发者页面上的信息是pretty苗条:
https://developer.chrome.com/multidevice/android/customtabs

它说,如果你绑定成功的自定义选项卡可以安全使用。有没有绑定到测试这个简单的方法?

喜欢我假设:

 意图serviceIntent =新意图(android.support.customtabs.action.CustomTabsService);
serviceIntent.setPackage(com.android.chrome);
布尔customTabsSupported = bindService(serviceIntent,新CustomTabsServiceConnection(){
            @覆盖
            公共无效onCustomTabsServiceConnected(最终组件名组件名,最后CustomTabsClient customTabsClient){}            @覆盖
            公共无效onServiceDisconnected(最终名称组件名){}
        },
        Context.BIND_AUTO_CREATE | Context.BIND_WAIVE_PRIORITY);如果(customTabsSupported){
    //支持
}


解决方案

而不是绑定和解除绑定的服务,您可以使用软件包管理系统,以检查是否支持自定义选项卡。

 私有静态最后弦乐SERVICE_ACTION =android.support.customtabs.action.CustomTabsService;
    私有静态最后弦乐CHROME_PACKAGE =com.android.chrome;    私有静态布尔isChromeCustomTabsSupported(@NonNull最终上下文的背景下){
        意图serviceIntent =新意图(SERVICE_ACTION);
        serviceIntent.setPackage(CHROME_PACKAGE);
        清单< ResolveInfo> 。resolveInfos = context.getPackageManager()queryIntentServices(serviceIntent,0);
        返回(resolveInfos == NULL || resolveInfos.isEmpty())!;
    }

请注意,其他浏览器可能支持自定义选项卡在未来,所以你可能要修改来支持这种情况。

I have an activity that loads an external url into a webview within my app. I'd like to use Chrome Custom tabs when it's available but I support devices that might not have a version of Chrome that supports them.

In the case of CustomTabs not being supported I'd like to use my old code but use the CustomTabsIntent.Builder() when they are. The old code loads the content in a WebView contained in an Activity where I can still manage the ActionBar.

I'd like to write a helper method that will tell me if it's supported but I'm not sure how. The info on the developer page is pretty slim: https://developer.chrome.com/multidevice/android/customtabs

It says if you bind succeeds the custom tabs can be safely used. Is there an easy way to bind to test this?

Like this I assume:

Intent serviceIntent = new Intent("android.support.customtabs.action.CustomTabsService");
serviceIntent.setPackage("com.android.chrome");
boolean customTabsSupported = bindService(serviceIntent, new CustomTabsServiceConnection() {
            @Override
            public void onCustomTabsServiceConnected(final ComponentName componentName, final CustomTabsClient customTabsClient) {}

            @Override
            public void onServiceDisconnected(final ComponentName name) {}
        },
        Context.BIND_AUTO_CREATE | Context.BIND_WAIVE_PRIORITY);

if (customTabsSupported) {
    // is supported
}

解决方案

Instead of binding and unbinding the service, you can use the PackageManager to check if Custom Tabs is supported.

  private static final String SERVICE_ACTION = "android.support.customtabs.action.CustomTabsService";
    private static final String CHROME_PACKAGE = "com.android.chrome";

    private static boolean isChromeCustomTabsSupported(@NonNull final Context context) {
        Intent serviceIntent = new Intent(SERVICE_ACTION);
        serviceIntent.setPackage(CHROME_PACKAGE);
        List<ResolveInfo> resolveInfos = context.getPackageManager().queryIntentServices(serviceIntent, 0);
        return !(resolveInfos == null || resolveInfos.isEmpty());
    }

Be aware that other browsers may support Custom Tabs in the future, so you may want to modify that to support this case.

这篇关于如何检查是否铬镀铬支持自定义选项卡?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆