如何检查Chrome是否支持Chrome自定义标签?

Jef*_*eff 9 android chrome-custom-tabs

我有一个活动将外部网址加载到我的应用内的网页浏览中.我想在可用时使用Chrome自定义标签,但我支持的设备可能没有支持它们的Chrome版本.

在不支持CustomTabs的情况下,我想使用我的旧代码但是在它们使用时使用CustomTabsIntent.Builder().旧代码将内容加载到Activity中包含的WebView中,我仍然可以在其中管理ActionBar.

我想写一个帮助方法,告诉我它是否支持,但我不知道如何.开发者页面上的信息非常简洁:https: //developer.chrome.com/multidevice/android/customtabs

它表示如果您成功绑定,则可以安全地使用自定义选项卡.有没有一种简单的方法可以绑定来测试它?

像这样我假设:

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
}
Run Code Online (Sandbox Code Playgroud)

and*_*ban 14

您可以使用PackageManager检查是否支持自定义选项卡,而不是绑定和解除绑定服务.

  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());
    }
Run Code Online (Sandbox Code Playgroud)

请注意,其他浏览器将来可能会支持自定义标签,因此您可能需要修改它以支持此案例.


And*_*dme 7

您可以尝试使用以下代码来确定您是否拥有支持自定义标签的浏览器:

private static final String TAG = "CustomTabLauncher";
static final String STABLE_PACKAGE = "com.android.chrome";
static final String BETA_PACKAGE = "com.chrome.beta";
static final String DEV_PACKAGE = "com.chrome.dev";
static final String LOCAL_PACKAGE = "com.google.android.apps.chrome";
String mPackageNameToUse;

private String getPackageName(Context context) {
    if (mPackageNameToUse != null) {
        return mPackageNameToUse;
    }

    // Get default VIEW intent handler that can view a web url.
    Intent activityIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.test-url.com"));

    // Get all apps that can handle VIEW intents.
    PackageManager pm = context.getPackageManager();
    List<ResolveInfo> resolvedActivityList = pm.queryIntentActivities(activityIntent, 0);
    List<String> packagesSupportingCustomTabs = new ArrayList<>();
    for (ResolveInfo info : resolvedActivityList) {
        Intent serviceIntent = new Intent();
        serviceIntent.setAction(CustomTabsService.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()) {
        mPackageNameToUse = null;
    } else if (packagesSupportingCustomTabs.size() == 1) {
        mPackageNameToUse = packagesSupportingCustomTabs.get(0);
    } else if (packagesSupportingCustomTabs.contains(STABLE_PACKAGE)) {
        mPackageNameToUse = STABLE_PACKAGE;
    } else if (packagesSupportingCustomTabs.contains(BETA_PACKAGE)) {
        mPackageNameToUse = BETA_PACKAGE;
    } else if (packagesSupportingCustomTabs.contains(DEV_PACKAGE)) {
        mPackageNameToUse = DEV_PACKAGE;
    } else if (packagesSupportingCustomTabs.contains(LOCAL_PACKAGE)) {
        mPackageNameToUse = LOCAL_PACKAGE;
    }
    return mPackageNameToUse;
}
Run Code Online (Sandbox Code Playgroud)

打电话时,你可以这样做:

public void openCustomTab(Uri uri, Activity activity) {
    //If we cant find a package name, it means there's no browser that supports
    //Chrome Custom Tabs installed. So, we fallback to the default browser
    if (getPackageName(activity) == null) {
        activity.startActivity(new Intent(Intent.ACTION_VIEW, uri));
    } else {
        CustomTabsIntent.Builder intentBuilder = new CustomTabsIntent.Builder();
        intentBuilder.enableUrlBarHiding();
        intentBuilder.setToolbarColor(activity.getResources().getColor(R.color.purple_a_01));

        CustomTabsIntent customTabsIntent = intentBuilder.build();
        customTabsIntent.intent.setPackage(mPackageNameToUse);

        customTabsIntent.launchUrl(activity, uri);
    }
}
Run Code Online (Sandbox Code Playgroud)


Jef*_*eff 6

我最终在我的Utils类中编写了一个静态方法,所以我可以检查并处理不支持它的情况:

/**
     * Check if Chrome CustomTabs are supported. 
     * Some devices don't have Chrome or it may not be
     * updated to a version where custom tabs is supported.
     *
     * @param context the context
     * @return whether custom tabs are supported
     */
    public static boolean isChromeCustomTabsSupported(@NonNull final Context context) {
        Intent serviceIntent = new Intent("android.support.customtabs.action.CustomTabsService");
        serviceIntent.setPackage("com.android.chrome");

        CustomTabsServiceConnection serviceConnection = new CustomTabsServiceConnection() {
            @Override
            public void onCustomTabsServiceConnected(final ComponentName componentName, final CustomTabsClient customTabsClient) { }

            @Override
            public void onServiceDisconnected(final ComponentName name) { }
        };

        boolean customTabsSupported =
                context.bindService(serviceIntent, serviceConnection, Context.BIND_AUTO_CREATE | Context.BIND_WAIVE_PRIORITY);
        context.unbindService(serviceConnection);

        return customTabsSupported;
    }
Run Code Online (Sandbox Code Playgroud)

  • 这会检查其他浏览器是否支持Chrome自定义标签? (2认同)

小智 5

如果您的目标是 API 级别 30 并在 Android 11 设备上运行,可能会出现新问题。

您需要在清单中添加一个部分,例如:

<queries>
    <intent>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data android:scheme="https" />
    </intent>
</queries>
Run Code Online (Sandbox Code Playgroud)

或者

<queries>
    <intent>
        <action android:name="android.support.customtabs.action.CustomTabsService" />
    </intent>
</queries>
Run Code Online (Sandbox Code Playgroud)

如果没有这个,上面文章中提到的一些对 PackageManager 的调用将无法按您的预期工作。

https://developer.android.com/training/package-visibility/use-cases