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

このガイドのパート 3 では、warmup() を使用してブラウザの起動を高速化し、mayLaunchUrl() を使用してウェブページをプリフェッチする方法に焦点を当てます。ブラウザのプロセスをウォームアップすると、リンクを開く際に最大 700 ミリ秒を節約できます。mayLaunchUrl でコンテンツをプリレンダリングすると、外部コンテンツがすぐに開きます。両方の API を組み合わせて使用すると、カスタムタブの統合のユーザー エクスペリエンスが大幅に向上するため、使用することを強くおすすめします。

必要な手順は次のとおりです。

  1. CustomTabsClient.getPackageName(...) で、デフォルトのブラウザがカスタムタブに対応しているかどうかを確認します。「はい」の場合は、CustomTabsClient.bindCustomTabsService() を介して CustomTabsService にバインドします。
  2. CustomTabsService に接続したら、CustomTabsServiceConnection.onCustomTabsServiceConnected() コールバックで次の操作を行います。

    a. CustomTabsClient.warmup() でブラウザ プロセスをウォームアップします。b. CustomTabsClient.newSession() を使用して新しい CustomTabsSession を作成します。

  3. 必要に応じて、ユーザーがアクセスする可能性の高いウェブページを CustomTabsSession.mayLaunchUrl() でプリフェッチします。

  4. 新しいカスタムタブを起動するときに、コンストラクタ new CustomTabsIntent.Builder(session) を介して CustomTabsSessionCustomTabsIntent.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 CustomTabsClient mClient;
private CustomTabsSession mSession;

private CustomTabsServiceConnection mConnection = new CustomTabsServiceConnection() {
    @Override
    public void onCustomTabsServiceConnected(
            @NonNull ComponentName name,
            @NonNull CustomTabsClient client
    ) {
        mClient = client;
        // Warm up the browser process
        mClient.warmup(0 /* placeholder for future use */);
        // Create a new browser session
        mSession = mClient.newSession(new CustomTabsCallback());
        // Pre-render pages the user is likely to visit
        // you can do this any time while the service is connected
        mSession.mayLaunchUrl(Uri.parse("https://developers.android.com"), null, null);
    }

    @Override
    public void onServiceDisconnected(ComponentName name) {
        mClient = null;
        mSession = null;
    }
};

private void bindCustomTabService(Context context) {
    // Check for an existing connection
    if (mClient != 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
    String packageName = CustomTabsClient.getPackageName(context, null);
    if (packageName == null) {
        // Do nothing as service connection is not supported
        return;
    }
    CustomTabsClient.bindCustomTabsService(context, packageName, mConnection);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
…
    bindCustomTabService(this);
    findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String url = "https://developers.android.com";
            CustomTabsIntent intent = new CustomTabsIntent.Builder(mSession)
                    .build();
            intent.launchUrl(MainActivity.this, Uri.parse(url));
        }
    });
}

ネイティブ アプリでウェブページを開く

Android では、URL はネイティブ アプリで処理できます。たとえば、Facebook アプリをインストールしているユーザーが Facebook の投稿へのリンクをクリックしたときに、ブラウザではなく Facebook アプリで開くリンクのほうが、通常は好まれます。

デフォルトでは、カスタムタブは各ネイティブ アプリケーション(インストールされている場合)でリンクを開きます。ただし、CustomTabsServiceConnection が設定されると、この動作は停止し、すべての URL がカスタムタブで開きます。ユーザー エクスペリエンスを向上させるために、次のコードを使用してこの動作を再度有効にすることをおすすめします。

CustomTabsIntent customTabsIntent = new CustomTabsIntent.Builder()
    .setSendToExternalDefaultHandlerEnabled(true)
    .build();

次のトピック: カスタムタブのサイズを変更する方法