自定义 chrome 标签要求多个浏览器选择

custom chrome tabs asks for multiple browser to choose

我正在尝试实现自定义 chrome 选项卡。我正在使用 Google 的默认库 customtabs.

我参考了 this 实现自定义 chrome 选项卡的教程。现在按照教程中的建议,我做了这样的编码。

mCustomTabsServiceConnection = new CustomTabsServiceConnection() {
    @Override
    public void onCustomTabsServiceConnected(
        ComponentName componentName,
        CustomTabsClient customTabsClient) {
        mCustomTabsClient = customTabsClient;
        mCustomTabsClient.warmup(0L);
        mCustomTabsSession = mCustomTabsClient.newSession(null);
    }

    @Override
    public void onServiceDisconnected(ComponentName name) {
        mCustomTabsClient = null;
    }
};

CustomTabsClient.bindCustomTabsService(this, "com.android.chrome",
        mCustomTabsServiceConnection);

CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder(
        mCustomTabsSession).setShowTitle(true);
builder.setToolbarColor(getResources().getColor(R.color.purple_main));
        builder.setStartAnimations(getApplicationContext(), R.anim.fadein,
                R.anim.fadeout);
builder.setExitAnimations(getApplicationContext(), R.anim.fadein,
                R.anim.fadeout);
mCustomTabsIntent = builder.build(); 

并启动自定义选项卡。

mCustomTabsIntent.launchUrl(TampleDetails.this,
       Uri.parse(temple_website));

现在如您所见,我已将自定义选项卡与 chrome 的包名绑定,但它仍然要求在 Chrome 和 [=28 之间进行选择=]UC浏览器。很明显UC浏览器不支持自定义标签页

所以我的问题是如何限制自定义选项卡仅与 Chrome 浏览器绑定?

任何帮助将不胜感激。

谢谢。

检查:如果您在启动url之前将CustomTabIntent包设置为所需浏览器的包,则可以跳过Android对话框浏览器选择;对我来说有效:

/**
 * Opens the URL on a Custom Tab
 *
 * @param activity         The host activity.
 * @param uri              the Uri to be opened.
 */
public static void openCustomTab(Activity activity,
                                 Uri uri) {
    // Here is a method that returns the chrome package name 
    String packageName = CustomTabsHelper.getPackageNameToUse(activity, mUrl);

    CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
    mCustomTabsIntent = builder
            .setShowTitle(true)
            .build();
    builder.setToolbarColor(ContextCompat.getColor(activity, R.color.colorPrimary));

    if ( packageName != null ) {
        mCustomTabsIntent.intent.setPackage(packageName);
    }
    mCustomTabsIntent.launchUrl(activity, uri);
}

支持多种浏览器

自定义标签协议是开放的,这意味着其他浏览器可以支持它。事实上,Samsung Internet Browser 已经支持它(尽管实现有问题)并且 Firefox 已经为其夜间构建添加了一个初始的、非常实验性的支持。

因此,最佳做法是查询 Android 系统,其中安装的浏览器支持自定义标签协议:

private static final String ACTION_CUSTOM_TABS_CONNECTION =
        "android.support.customtabs.action.CustomTabsService";

public static ArrayList<ResolveInfo> getCustomTabsPackages(Context context) {
    PackageManager pm = context.getPackageManager();
    // Get default VIEW intent handler.
    Intent activityIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.example.com"));

    // Get all apps that can handle VIEW intents.
    List<ResolveInfo> resolvedActivityList = pm.queryIntentActivities(activityIntent, 0);
    ArrayList<ResolveInfo> 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);
        }
    }

    return packagesSupportingCustomTabs;
}

您可能需要检查 ResolveInfo#preferredOrder 以检查用户是否对其中一个应用程序的偏好高于其他应用程序。此外,如果没有首选应用(或两个应用具有相同的主要偏好级别),您可能希望让用户选择一个,或者使用合理的默认值

考虑原生应用

检查给定 Url 是否安装了处理它的本机应用程序也很重要。如果是这样,您的应用程序启动原生应用程序而不是在自定义选项卡中打开它可能是有意义的:

public static List<ResolveInfo> getNativeApp(Context context, Uri uri) {
    PackageManager pm = context.getPackageManager();

    //Get all Apps that resolve a random url
    Intent browserActivityIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.example.com"));
    List<ResolveInfo> resolvedBrowserList = pm.queryIntentActivities(browserActivityIntent, 0);

    Intent specializedActivityIntent = new Intent(Intent.ACTION_VIEW, uri);
    List<ResolveInfo> resolvedSpecializedList = pm.queryIntentActivities(specializedActivityIntent, 0);
    resolvedSpecializedList.removeAll(resolvedBrowserList);

    return resolvedBrowserList;

}

打开带有特定包的自定义选项卡

决定使用哪个处理程序打开自定义选项卡后,使用 mCustomTabsIntent.intent.setPackage(packageName); 告诉自定义选项卡打开哪个应用程序。

可能会有帮助。

    CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
    CustomTabsIntent customTabsIntent = builder.build();

    customTabsIntent.intent.setPackage("com.android.chrome");
    builder.setToolbarColor(ContextCompat.getColor(context,R.color.colorPrimary));
    builder.setShowTitle(true);
    builder.addDefaultShareMenuItem();

    builder.build().launchUrl((Activity) context, Uri.parse(http_link));

你可以通过像这样指定包名来避免这个问题 intentCustomTabs.intent.setPackage("com.android.chrome");

完整代码:

    String url = link;
    CustomTabsIntent.Builder builderCustomTabs = new CustomTabsIntent.Builder();
    CustomTabsIntent intentCustomTabs = builderCustomTabs.build();
    intentCustomTabs.intent.setPackage("com.android.chrome"); 
    intentCustomTabs.intent.addFlags(new Integer("67108864"));
    builderCustomTabs.setShowTitle(true); 
    builderCustomTabs.setToolbarColor(ContextCompat.getColor(this, R.color.colorPrimary));
    builderCustomTabs.enableUrlBarHiding();
    intentCustomTabs.launchUrl(this, Uri.parse(url));