简体   繁体   English

Android 应用链接 - 从应用打开 URL 到自定义 chrome 标签

[英]Android app link - Open URL from app to custom chrome tab

I have enabled the app link for my app, in some scenarios I want open URL custom chrome tab for some URLs.我已为我的应用启用了应用链接,在某些情况下,我希望为某些 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));

As suggested in this flowing code can be used open chrome browser is there any way to do the same with custom chrome tab如此流动代码中所建议的,可以使用打开的chrome 浏览器是否有任何方法可以对custom chrome tab执行相同的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);

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更多信息https://developers.google.com/web/android/custom-tabs/best-practices#preparing_for_other_browsers

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试使用来自另一个 android 应用程序的 URL 链接打开 chrome 应用程序 - Trying to open chrome app with a URL link from another android app Android 6-无法从Chrome打开应用网址 - Android 6 - cannot open app url from chrome 尝试从Android应用程序内部打开在Chrome中打开的外部URL的链接 - Trying to open a link for an external URL open in Chrome from inside an Android App 从Chrome自定义标签重定向到Android应用时,“导航被屏蔽” - “Navigation is blocked” when redirecting from Chrome Custom Tab to Android app android:从片段打开chrome自定义选项卡 - android : open chrome custom tab from fragment Android 应用程序链接 - 在浏览器中从应用程序打开 url 而不触发应用程序链接 - Android App link - Open a url from app in browser without triggering App Link 从URL打开应用程序适用于Android版Firefox但不适用于谷歌浏览器 - Open App from URL works on Firefox for Android but not on Google Chrome 通过链接打开android应用 - Open android app from link Android Chrome浏览器“自定义”标签-在新标签+ Postmessage中打开链接 - Android Chrome Custom Tabs - Open link in new tab + Postmessage Android上的Chrome浏览器中的Meteor应用程序打开URL - Meteor app open URL in chrome browser on Android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM