暖機和預先擷取:使用自訂分頁服務

本指南的第三部分著重於自訂分頁服務,以及在應用程式中使用這項服務可提升使用者體驗的原因:

  • 立即開啟外部內容:使用 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 上,網址可由 Android 應用程式處理。舉例來說,如果使用者已安裝 Facebook 應用程式,並點選 Facebook 貼文連結,通常會希望連結在 Facebook 應用程式中開啟,而不是在瀏覽器中開啟。

根據預設,如果已安裝對應的 Android 應用程式,自訂分頁會在該應用程式中開啟連結。不過,一旦建立 CustomTabsServiceConnection,這項行為就會停止運作,所有網址都會改為在自訂分頁中開啟。為改善使用者體驗,建議您使用下列程式碼重新啟用這項行為:

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

接下來:瞭解如何調整自訂分頁體驗的大小。