อุ่นเครื่องและดึงข้อมูลล่วงหน้า: ใช้บริการแท็บที่กำหนดเอง

ส่วนที่ 3 ของคู่มือนี้จะเน้นไปที่บริการแท็บที่กำหนดเอง และเหตุผลที่การใช้บริการดังกล่าวในแอปพลิเคชันสร้างประสบการณ์ของผู้ใช้ที่ดีขึ้น

  • เปิดเนื้อหาภายนอกได้ทันที: การใช้ warmup() จะทำให้กระบวนการของเบราว์เซอร์เริ่มต้นในเบื้องหลังแม้ก่อนที่ผู้ใช้จะคลิกลิงก์และจะบันทึกได้สูงสุด 700 มิลลิวินาทีเมื่อเปิดลิงก์ mayLaunchUrl() ดึงข้อมูลหน้าล่วงหน้า การใช้ API ทั้งสองร่วมกันจะช่วยให้หน้าเว็บโหลดได้ทันที ซึ่งช่วยปรับปรุงประสบการณ์ของผู้ใช้สำหรับการผสานรวมแท็บที่กำหนดเองได้เป็นอย่างมาก
  • จัดการแท็บที่กำหนดเองที่ย่อขนาดได้ดียิ่งขึ้น: ด้วยการเชื่อมต่อกับบริการแท็บที่กำหนดเองและใช้ CustomTabSession เดียวกันเมื่อเปิดแท็บที่กำหนดเอง Chrome จะสามารถนำแท็บที่กำหนดเองซึ่งย่อขนาดไว้ก่อนหน้าออกก่อนเปิดแท็บใหม่ได้ เพื่อให้ผู้ใช้ได้รับประสบการณ์ที่สอดคล้องกันยิ่งขึ้น

ขั้นตอนที่จำเป็นมีดังนี้

  1. โดยใช้ CustomTabsClient.getPackageName(...) ตรวจสอบว่าเบราว์เซอร์เริ่มต้นรองรับแท็บที่กำหนดเองหรือไม่ หากใช่ ให้เชื่อมโยงกับ CustomTabsService โดยใช้ CustomTabsClient.bindCustomTabsService()
  2. เมื่อเชื่อมต่อ CustomTabsService แล้ว ในโค้ดเรียกกลับ CustomTabsServiceConnection.onCustomTabsServiceConnected() ให้ทําดังนี้

    a. ทำให้กระบวนการของเบราว์เซอร์ทำงานอุ่นขึ้นโดยใช้ CustomTabsClient.warmup() b. สร้าง CustomTabsSession ใหม่โดยใช้ CustomTabsClient.newSession()

  3. ดึงข้อมูลหน้าเว็บที่ผู้ใช้น่าจะเข้าชมล่วงหน้าโดยใช้ CustomTabsSession.mayLaunchUrl() (ไม่บังคับ)

  4. เมื่อเปิดแท็บที่กำหนดเองใหม่ ให้ส่ง 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 จะจัดการ URL ได้ เช่น หากผู้ใช้ติดตั้งแอป Facebook ไว้และคลิกลิงก์ไปยังโพสต์ Facebook ผู้ใช้มักจะเลือกเปิดลิงก์ในแอป Facebook แทนในเบราว์เซอร์

โดยค่าเริ่มต้น แท็บที่กำหนดเองจะเปิดลิงก์ในแอปพลิเคชัน Android ที่เกี่ยวข้องหากติดตั้งไว้ แต่เมื่อสร้าง CustomTabsServiceConnection แล้ว ลักษณะการทำงานนี้จะหยุดทำงานและ URL ทั้งหมดจะเปิดในแท็บที่กำหนดเองแทน เพื่อให้ผู้ใช้ได้รับประสบการณ์ที่ดีขึ้น เราขอแนะนำให้เปิดใช้ลักษณะการทำงานนี้อีกครั้งโดยใช้โค้ดต่อไปนี้

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

ขั้นตอนถัดไป: ดูวิธีปรับขนาดประสบการณ์การใช้งานแท็บที่กำหนดเอง