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