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

Open CustomTabIntent with AppLink enabled application

我正在尝试在我的应用程序中添加 CustomTabIntent 以打开我的网站 url。问题是我已经在我的应用程序中实现了 AppLinking,因此 Chrome 选项卡没有出现,它被重定向到我的深层链接处理程序 class,它将我的 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));

他们是我绕过应用链接打开 CustomTabIntent 的方法吗?

是的,您可以通过访问CustomTabsIntent中的内部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 等其他浏览器也支持 CustomTabs,理想情况下,您最好找出安装了哪些并使用其中之一。

以下代码可以提供帮助:

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

可以将它与 CustomTabsClient.getPackageName 结合使用到 select 一个包。

感谢安德烈班​​ and , I wrote the same in Kotlin. Updated thanks to

private fun getCustomTabsPackages(context: Context, url: String): List<ResolveInfo> {
    val pm: PackageManager = context.packageManager
    // Get default VIEW intent handler.
    val activityIntent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
    // Get all apps that can handle VIEW intents.
    val resolvedActivityList: List<ResolveInfo> = pm.queryIntentActivities(activityIntent, 0)
    return resolvedActivityList.filter {
        val serviceIntent = Intent()
        serviceIntent.action = ACTION_CUSTOM_TABS_CONNECTION
        serviceIntent.setPackage(it.activityInfo.packageName)
        // Check if this package also resolves the Custom Tabs service.
        pm.resolveService(serviceIntent, 0) != null
    }
}

private fun showBrowser(url: String) {
    val customTabsPackages = getCustomTabsPackages(context!!, url)
    if (customTabsPackages.isNullOrEmpty()) {
        openAnyBrowser(url)
    } else {
        // Get the first Chrome-compatible browser from the list.
        val packageName = customTabsPackages.first().activityInfo.packageName
        openChromeTabs(packageName, url)
    }
}

private fun openAnyBrowser(url: String) {
    val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
    if (intent.resolveActivity(context!!.packageManager) != null) {
        startActivity(intent)
    } else {
        showError("Cannot open web browser")
    }
}

private fun openChromeTabs(packageName: String, url: String) {
    val params = CustomTabColorSchemeParams.Builder()
        .setToolbarColor(ContextCompat.getColor(context, R.color.colorPrimary))
        .setSecondaryToolbarColor(ContextCompat.getColor(context, R.color.colorPrimaryDark))
        .setNavigationBarColor(ContextCompat.getColor(context, R.color.colorPrimary))
        .build()
    val builder = CustomTabsIntent.Builder()
        .setColorScheme(CustomTabsIntent.COLOR_SCHEME_DARK)
        .setColorSchemeParams(CustomTabsIntent.COLOR_SCHEME_DARK, params)
        //.setDefaultColorSchemeParams(params)
        .setShowTitle(true)
        .setShareState(SHARE_STATE_DEFAULT)
        .setStartAnimations(context, R.anim.slide_in_right, R.anim.slide_out_left)
        .setExitAnimations(context, android.R.anim.slide_in_left,
            android.R.anim.slide_out_right)
    val customTabsIntent = builder.build()
    customTabsIntent.intent.setPackage(packageName)
    customTabsIntent.launchUrl(context!!, Uri.parse(url))
}

build.gradle中:

// Chrome Custom Tabs.
implementation 'androidx.browser:browser:1.3.0'

在API30intent.resolveActivity(context!!.packageManager),所以,在AndroidManifest中添加这些行,如果你想显示其他浏览器:

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