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

时间:2016-07-18 06:10:51

标签: android chrome-custom-tabs

我正在尝试实施自定义Chrome标签。我使用的是谷歌的默认库 customtabs

我提到了this教程来实现自定义Chrome标签。现在按照教程中的建议,我做了类似的编码。

struct BufferData
{
    GLfloat camera_position[4];
    GLfloat light_position[4];
    GLfloat light_diffuse[4];
};

并启动自定义标签。

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(); 

现在,当您看到我已将自定义标签与Chrome包的名称绑定时,它仍然要求在 Chrome UC浏览器之间进行选择。很明显,UC浏览器不支持自定义标签。

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

任何帮助将不胜感激。

谢谢。

4 个答案:

答案 0 :(得分:14)

检查this stackoverflow answer:如果在启动网址之前将CustomTabIntent软件包与所需浏览器的软件包一起设置,则可以跳过Android对话框浏览器选项;对我而言,它起作用了:

ALTER PROCEDURE [dbo].[proc_PostVisitCountInsert] (
    @postCount BIGINT
    ,@postID BIGINT
    )
AS
BEGIN
    IF NOT EXISTS (
            SELECT 1
            FROM PostVisitCount
            WHERE postID = @postID
            )
    BEGIN
        INSERT INTO PostVisitCount (
            postcount
            ,postID
            )
        VALUES (
            @postCount
            ,@postID
            )
    END
    ELSE
    BEGIN
        --Query for Update PostVisitCount
        DECLARE @count BIGINT

        SET @count = (
                SELECT sum(postcount) + 1
                FROM PostVisitCount
                WHERE postID = @postID
                )

        UPDATE PostVisitCount
        SET postcount = @count
        WHERE postID = @postID
    END
END

答案 1 :(得分:8)

支持多种浏览器

自定义标签协议已打开,这意味着其他浏览器可以支持它。事实上,三星互联网浏览器已经支持它(尽管实施方式已经破损),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);告诉自定义选项卡打开它的应用程序。

答案 2 :(得分:2)

可能会有所帮助。

    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));

答案 3 :(得分:0)

您可以通过指定包名称来避免此问题,例如此intentCustomTabs.intent.setPackage(&#34; com.android.chrome&#34;);

完整代码:

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