1

我已经为我的应用启用了应用链接,在某些情况下,我想为某些 URL 打开 URL自定义 chrome 选项卡

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

正如此流动代码中所建议的,可以使用打开的chrome 浏览器,有什么方法可以做到这一点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);
4

1 回答 1

0

您需要将包信息设置为默认自定义选项卡处理程序的意图。(如铬)

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
}

更多信息https://developers.google.com/web/android/custom-tabs/best-practices#preparing_for_other_browsers

于 2020-11-03T10:10:48.040 回答