ส่วนที่ 3 ของคู่มือนี้จะเน้นที่บริการ Custom Tabs และเหตุผลที่การใช้บริการนี้ในแอปพลิเคชันของคุณจะสร้างประสบการณ์ของผู้ใช้ที่ดียิ่งขึ้น
- เปิดเนื้อหาภายนอกทันที: การใช้
warmup()จะทำให้กระบวนการของเบราว์เซอร์เริ่มทำงานในเบื้องหลังแม้ว่าผู้ใช้จะยังไม่ได้คลิกลิงก์ และช่วยประหยัดเวลาได้สูงสุด 700 มิลลิวินาทีเมื่อเปิดลิงก์mayLaunchUrl()จะดึงข้อมูลหน้าเว็บล่วงหน้า การใช้ API ทั้ง 2 รายการร่วมกันจะช่วยให้หน้าเว็บโหลดได้ทันที ซึ่งจะช่วยปรับปรุงประสบการณ์ของผู้ใช้ในการผสานรวมแท็บที่กำหนดเองได้อย่างมาก - การจัดการแท็บที่กำหนดเองที่ย่อให้เล็กลงได้ดีขึ้น: การเชื่อมต่อกับบริการแท็บที่กำหนดเองและใช้
CustomTabSessionเดียวกันเมื่อเปิดแท็บที่กำหนดเองจะช่วยให้ Chrome นำแท็บที่กำหนดเองที่ย่อให้เล็กลงก่อนหน้านี้ออกได้ก่อนที่จะเปิดแท็บใหม่ ซึ่งจะมอบประสบการณ์ของผู้ใช้ที่สอดคล้องกันมากขึ้น
ขั้นตอนที่ต้องทำมีดังนี้
- ตรวจสอบว่าเบราว์เซอร์เริ่มต้นรองรับแท็บที่กำหนดเองหรือไม่โดยใช้
CustomTabsClient.getPackageName(...)หากใช่ ให้เชื่อมโยงกับ CustomTabsService โดยใช้CustomTabsClient.bindCustomTabsService() เมื่อเชื่อมต่อกับ CustomTabsService แล้ว ให้ทำดังนี้ใน Callback
CustomTabsServiceConnection.onCustomTabsServiceConnected()ก. เตรียมกระบวนการของเบราว์เซอร์โดยใช้
CustomTabsClient.warmup()ข. สร้างCustomTabsSessionใหม่โดยใช้CustomTabsClient.newSession()หรือจะดึงข้อมูลหน้าเว็บที่ผู้ใช้มีแนวโน้มจะเข้าชมล่วงหน้าโดยใช้
CustomTabsSession.mayLaunchUrl()ก็ได้เมื่อเปิด Custom Tab ใหม่ ให้ส่ง
CustomTabsSessionไปยัง CustomTabsIntent.Builder โดยใช้คอนสตรัคเตอร์CustomTabsIntent.Builder(session)
หากแอปกำหนดเป้าหมายเป็น Android API ระดับ 30 CustomTabsClient.getPackageName(...) จะกำหนดให้คุณเพิ่มส่วนการค้นหาลงในไฟล์ Manifest ของ Android โดยประกาศตัวกรอง Intent ที่ตรงกับเบราว์เซอร์ที่รองรับแท็บที่กำหนดเอง
<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 แอปพลิเคชัน Android จะจัดการ URL ได้ ตัวอย่างเช่น หากผู้ใช้ติดตั้งแอป Facebook ไว้และคลิกลิงก์ไปยังโพสต์ Facebook โดยปกติแล้วผู้ใช้จะต้องการให้ลิงก์เปิดในแอป Facebook แทนที่จะเปิดในเบราว์เซอร์
โดยค่าเริ่มต้น แท็บที่กำหนดเองจะเปิดลิงก์ในแอปพลิเคชัน Android ที่เกี่ยวข้องหากติดตั้งไว้ อย่างไรก็ตาม เมื่อสร้าง CustomTabsServiceConnection แล้ว ลักษณะการทำงานนี้จะหยุดทำงานและ URL ทั้งหมดจะเปิดในแท็บที่กำหนดเองแทน เราขอแนะนำให้เปิดใช้ลักษณะการทำงานนี้อีกครั้งโดยใช้โค้ดต่อไปนี้ เพื่อปรับปรุงประสบการณ์ของผู้ใช้
val customTabsIntent = CustomTabsIntent.Builder()
.setSendToExternalDefaultHandlerEnabled(true)
.build()