Android 使用启用AppLink的应用程序打开CustomTabIntent

Android 使用启用AppLink的应用程序打开CustomTabIntent,android,applinks,chrome-custom-tabs,Android,Applinks,Chrome Custom Tabs,我正在尝试将CustomTabIntent添加到我的应用程序中,以打开我网站的url。问题是我已经在我的应用程序中实现了AppLinking,因为Chrome选项卡没有出现,它被重定向到我的deeplink处理程序类,该类将我的url重定向到Chrome 我在CustomTabIntent中使用了以下代码 CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder() .setToolbarColor(C

我正在尝试将CustomTabIntent添加到我的应用程序中,以打开我网站的url。问题是我已经在我的应用程序中实现了AppLinking,因为Chrome选项卡没有出现,它被重定向到我的deeplink处理程序类,该类将我的url重定向到Chrome

我在CustomTabIntent中使用了以下代码

CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder()
            .setToolbarColor(ContextCompat.getColor(this, R.color.colorPrimary))
            .setSecondaryToolbarColor(ContextCompat.getColor(this, R.color.colorPrimaryDark))
            .setShowTitle(true)
            .addDefaultShareMenuItem()
            .setStartAnimations(this, R.anim.slide_in_right, R.anim.slide_out_left)
            .setExitAnimations(this, android.R.anim.slide_in_left,
                    android.R.anim.slide_out_right);
    CustomTabsIntent customTabsIntent = builder.build();
    customTabsIntent.launchUrl(this, Uri.parse(url));

这是我绕过applink打开CustomTabIntent的一种方法吗?

是的,您可以通过访问CustomTabInt中的内部Intent并调用setPackageName来设置要打开的应用程序的包名

String packageName = "...";
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder()
        .setToolbarColor(ContextCompat.getColor(this, R.color.colorPrimary))
        .setSecondaryToolbarColor(ContextCompat.getColor(this, R.color.colorPrimaryDark))
        .setShowTitle(true)
        .addDefaultShareMenuItem()
        .setStartAnimations(this, R.anim.slide_in_right, R.anim.slide_out_left)
        .setExitAnimations(this, android.R.anim.slide_in_left,
                android.R.anim.slide_out_right);
CustomTabsIntent customTabsIntent = builder.build();
customTabsIntent.intent.setPackage(packageName);
customTabsIntent.launchUrl(this, Uri.parse(url));
从现在开始,您一定想知道如何在打开“自定义”选项卡时确定要设置的包名称。即使您可以为特定浏览器硬编码软件包名称,例如:

customTabsIntent.intent.setPackageName("com.android.chrome");
但是,由于Firefox和Samsung等其他浏览器也支持CustomTab,因此理想情况下,您需要找出安装了哪些浏览器并使用其中的一个

以下代码可以帮助实现这一点:

public static ArrayList 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 resolvedActivityList = pm.queryIntentActivities(activityIntent, 0);
    ArrayList packagesSupportingCustomTabs = new ArrayList<>();
    for (ResolveInfo info : resolvedActivityList) {
        Intent serviceIntent = new Intent();
        serviceIntent.setAction(ACTION_CUSTOM_TABS_CONNECTION);
        serviceIntent.setPackage(info.activityInfo.packageName);
        // Check if this package also resolves the Custom Tabs service.
        if (pm.resolveService(serviceIntent, 0) != null) {
            packagesSupportingCustomTabs.add(info);
        }
    }
    return packagesSupportingCustomTabs;
}
publicstaticarraylistgetcustomtabspackages(上下文){
PackageManager pm=context.getPackageManager();
//获取默认视图意图处理程序。
Intent activityIntent=新的Intent(Intent.ACTION\u视图,Uri.parse(“http://www.example.com"));
//获取所有能够处理视图意图的应用程序。
List resolvedActivityList=pm.QueryEntActivities(activityIntent,0);
ArrayList PackageSupportingCustomTabs=新建ArrayList();
对于(ResolveInfo:resolvedActivityList){
意向服务意向=新意向();
serviceIntent.setAction(操作\自定义\选项卡\连接);
setPackage(info.activityInfo.packageName);
//检查此包是否也解析自定义选项卡服务。
if(pm.resolveService(serviceIntent,0)!=null){
PackageSupportingCustomTabs.add(信息);
}
}
返回包支持自定义选项卡;
}
可以将它与选择包结合使用。

多亏了andreban,我在Kotlin中也写了同样的内容。更新感谢

在API 30
intent.resolveActivity(context!!.packageManager)
中,如果要显示其他浏览器,请在
AndroidManifest
中添加以下行:

<queries>
    <intent>
        <action android:name="android.intent.action.VIEW" />
        <data android:scheme="http" />
    </intent>
</queries>


谢谢!看来有个解决方案可以试试。谢谢!
getCustomTabsPackages()
:。
setToolbarColor
setSecondaryToolbarColor
的源代码已被弃用,现在怎么办?@Arghadip,谢谢,我看到了你的主题:。
// Chrome Custom Tabs.
implementation 'androidx.browser:browser:1.3.0'
<queries>
    <intent>
        <action android:name="android.intent.action.VIEW" />
        <data android:scheme="http" />
    </intent>
</queries>