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

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

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

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

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

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

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