ウォームアップとプリフェッチ: カスタムタブ サービスの使用

このガイドの 3 番目のパートでは、カスタムタブ サービスと、アプリでカスタムタブ サービスを使用することでユーザー エクスペリエンスが向上する理由について説明します。

  • 外部コンテンツをすぐに開く: 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 マニフェストにクエリ セクションを追加し、カスタムタブをサポートするブラウザに一致するインテント フィルタを宣言する必要があります。

<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 では、URL を Android アプリで処理できます。たとえば、Facebook アプリをインストールしているユーザーが Facebook の投稿へのリンクをクリックした場合、通常はブラウザではなく Facebook アプリでリンクが開くことを好みます。

デフォルトでは、カスタムタブは、インストールされている場合、それぞれの Android アプリケーションでリンクを開きます。ただし、CustomTabsServiceConnection が確立されると、この動作は機能しなくなり、代わりにすべての URL がカスタムタブで開きます。ユーザー エクスペリエンスを向上させるため、次のコードを使用してこの動作を再度有効にすることをおすすめします。

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

次のステップ: カスタムタブのエクスペリエンスのサイズを変更する方法を学習する。