【问题标题】:custom chrome tabs asks for multiple browser to choose自定义 chrome 选项卡要求选择多个浏览器
【发布时间】:2016-07-18 06:10:51
【问题描述】:

我正在尝试实现自定义 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 的包名称绑定,但它仍然要求在 ChromeUC 浏览器 之间进行选择。而且很明显UC浏览器不支持自定义标签。

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

我们将不胜感激。

谢谢。

【问题讨论】:

  • 您正在测试哪个 Android 版本?
  • @ValentinoS。果冻豆4.2.2

标签: android chrome-custom-tabs


【解决方案1】:

你可以通过像这样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));

【讨论】:

    【解决方案2】:

    支持多种浏览器

    自定义标签协议是开放的,这意味着其他浏览器可以支持它。事实上,三星互联网浏览器已经支持它(尽管实现不完善),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 以检查用户是否偏好其中一个应用程序而不是其他应用程序。此外,如果没有首选应用程序(或两个应用程序具有相同的主要偏好级别),您可能需要让用户选择一个,或者使用合理的默认值

    考虑原生应用

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

    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); 告诉自定义选项卡打开哪个应用程序。

    【讨论】:

    • 我看到安装在某些设备上的 chrome 和 firefox 浏览器没有被以下代码选中: Intent serviceIntent = new Intent(); serviceIntent.setAction(ACTION_CUSTOM_TABS_CONNECTION); serviceIntent.setPackage(info.activityInfo.packageName); if (pm.resolveService(serviceIntent, 0) != null) { packagesSupportingCustomTabs.add(info); } 有什么想法吗?
    【解决方案3】:

    检查this stackoverflow answer:如果在启动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);
    }
    

    【讨论】:

    • mCustomTabsIntent.intent.setPackage(packageName); 行导致Activity not found 错误
    • 尝试检查之前是否为空: if ( packageName != null ) { mCustomTabsIntent.intent.setPackage(packageName); }
    【解决方案4】:

    可能会有所帮助。

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

    【讨论】:

    • 此代码仅在您使用此特定包名称安装 chrome 时才有效,否则您的应用会崩溃。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-06
    • 2012-03-14
    • 2020-05-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多