2

I have enabled the app link for my app, in some scenarios I want open URL custom chrome tab for some URLs.

CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
        CustomTabsIntent customTabsIntent = builder.build();
        builder.setShowTitle(true);
        Bundle headers = new Bundle();
customTabsIntent.intent.putExtra(Browser.EXTRA_HEADERS, headers);
        customTabsIntent.intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        customTabsIntent.launchUrl(context, Uri.parse(url));

As suggested in this flowing code can be used open chrome browser is there any way to do the same with custom chrome tab

String data = "example.com/your_url?param=some_param"
Intent defaultBrowser = 
Intent.makeMainSelectorActivity(Intent.ACTION_MAIN, 
Intent.CATEGORY_APP_BROWSER);
defaultBrowser.setData(data);
startActivity(defaultBrowser);
1
  • did you manage to find a solution? i am encountering the exact the issue. i tried the solution from the answer but it doesn't seem to work :( Commented May 29, 2024 at 14:46

1 Answer 1

0

You need to set package information to the intent of default custom tab handler. (Like chrome)

val customTabsIntent: CustomTabsIntent = Builder()
      ...
      .build()

customTabsIntent.intent.setPackage(getCustomTabsPackage(context))
private fun getCustomTabsPackage(context: Context): String? {
   val packageManager: PackageManager = context.packageManager
  val activityIntent = Intent(Intent.ACTION_VIEW, Uri.parse("http://"))
  val resolvedActivityList: List<ResolveInfo> = packageManager.queryIntentActivities(activityIntent, 0)

  return resolvedActivityList
    .firstOrNull {
      val serviceIntent = Intent()
      serviceIntent.action = CustomTabsService.ACTION_CUSTOM_TABS_CONNECTION
      serviceIntent.setPackage(it.activityInfo.packageName)
      packageManager.resolveService(serviceIntent, 0) != null
    }
    ?.activityInfo
    ?.packageName
}

More information https://developers.google.com/web/android/custom-tabs/best-practices#preparing_for_other_browsers

Sign up to request clarification or add additional context in comments.

1 Comment

thanks for the idea. it unfortunately doesn't seem to work. ps: we can also simplify the logic with the provided CustomTabsClient#getPackageName and even further by setting it in CustomTabsClient#bindCustomTabsService without having to modify the intent itself

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.