预热和预提取:使用自定义标签页服务

本指南的第三部分重点介绍自定义标签页服务,以及为什么在应用中使用该服务可以打造更好的用户体验:

  • 即时打开外部内容:使用 warmup() 会导致浏览器进程在用户点击链接之前就在后台启动,这样在打开链接时最多可节省 700 毫秒。mayLaunchUrl() 会预提取网页。同时使用这两个 API 可让网页即时加载,从而大大提升自定义标签页集成的用户体验。
  • 更好地处理最小化的自定义标签页:通过连接到自定义标签页服务并在启动自定义标签页时使用相同的 CustomTabSession,Chrome 将能够在启动新标签页之前移除之前最小化的自定义标签页,从而提供更一致的用户体验。

所需步骤如下:

  1. 使用 CustomTabsClient.getPackageName(...) 检查默认浏览器是否支持自定义标签页。如果是,请使用 CustomTabsClient.bindCustomTabsService() 绑定到 CustomTabsService。
  2. 连接到 CustomTabsService 后,在 CustomTabsServiceConnection.onCustomTabsServiceConnected() 回调中执行以下操作:

    a. 使用 CustomTabsClient.warmup() 预热浏览器进程。 b. 使用 CustomTabsClient.newSession() 创建新的 CustomTabsSession。

  3. (可选)使用 CustomTabsSession.mayLaunchUrl() 预提取用户可能访问的网页。

  4. 启动新的自定义标签页时,使用构造函数 CustomTabsIntent.Builder(session) 将 CustomTabsSession 传递给 CustomTabsIntent.Builder。

如果您的应用以 Android API 级别 30 为目标平台,CustomTabsClient.getPackageName(...) 会要求您向 Android 清单添加查询部分,以声明与支持自定义标签页的浏览器匹配的 intent-filter。

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">
  …
   <queries>
        <intent>
            <action android:name="android.support.customtabs.action.CustomTabsService" />
        </intent>
    </queries>
</manifest>

以下是有关如何连接到自定义标签页服务的完整示例:

private var customTabsClient: CustomTabsClient? = null
private var customTabsSession: CustomTabsSession? = null

private val connection = object : CustomTabsServiceConnection() {
    override fun onCustomTabsServiceConnected(
        name: ComponentName,
        client: CustomTabsClient
    ) {
        customTabsClient = client
        // Warm up the browser process
        client.warmup(0 /* placeholder for future use */)
        // Create a new browser session
        customTabsSession = client.newSession(CustomTabsCallback())
        // Pre-render pages the user is likely to visit
        // you can do this any time while the service is connected
        customTabsSession?.mayLaunchUrl(Uri.parse("https://developers.android.com"), null, null)
    }

    override fun onServiceDisconnected(name: ComponentName) {
        customTabsClient = null
        customTabsSession = null
    }
}

private fun bindCustomTabService(context: Context) {
    // Check for an existing connection
    if (customTabsClient != null) {
        // Do nothing if there is an existing service connection
        return
    }

    // Get the default browser package name, this will be null if
    // the default browser does not provide a CustomTabsService
    val packageName = CustomTabsClient.getPackageName(context, null)
        // Do nothing as service connection is not supported
        ?: return
    CustomTabsClient.bindCustomTabsService(context, packageName, connection)
}

override fun onCreate(savedInstanceState: Bundle?) {
    // …
    bindCustomTabService(this)
    findViewById<View>(R.id.button).setOnClickListener {
        val url = "https://developers.android.com"
        val intent = CustomTabsIntent.Builder(customTabsSession)
            .build()
        intent.launchUrl(this@MainActivity, Uri.parse(url))
    }
}

在 Android 上,网址可由 Android 应用处理。例如,如果用户安装了 Facebook 应用,并点击了指向 Facebook 帖子的链接,他们通常希望在 Facebook 应用中打开该链接,而不是在浏览器中打开。

默认情况下,如果已安装相应的 Android 应用,自定义标签页会在该应用中打开链接。不过,一旦建立了 CustomTabsServiceConnection,此行为就会停止,所有网址都会在自定义标签页中打开。为了提升用户体验,我们建议您使用以下代码重新启用此行为:

val customTabsIntent = CustomTabsIntent.Builder()
    .setSendToExternalDefaultHandlerEnabled(true)
    .build()

下一篇:了解如何调整自定义标签页体验的大小。