Sử dụng API cấp thấp của Tab tuỳ chỉnh

Bạn nên sử dụng Thư viện trình duyệt AndroidX để tích hợp ứng dụng với Thẻ tuỳ chỉnh. Tuy nhiên, bạn cũng có thể khởi động Thẻ tuỳ chỉnh mà không cần có thư viện hỗ trợ. Hướng dẫn này cung cấp thông tin tổng quan về cách đạt được mục tiêu này.

Bạn có thể triển khai đầy đủ Thư viện hỗ trợ trên GitHub và có thể dùng làm điểm xuất phát. Thư viện này cũng chứa các tệp AIDL cần thiết để kết nối với dịch vụ, vì các tệp này có trong kho lưu trữ Chromium không thể sử dụng trực tiếp được bằng Android Studio.

Thông tin cơ bản để khởi chạy thẻ tuỳ chỉnh bằng API cấp thấp

// Using a VIEW intent for compatibility with any other browsers on device.
// Caller should not be setting FLAG_ACTIVITY_NEW_TASK or 
// FLAG_ACTIVITY_NEW_DOCUMENT. 
String url = ¨https://paul.kinlan.me/¨;
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); 
//  Must have. Extra used to match the session. Its value is an IBinder passed
//  whilst creating a news session. See newSession() below. Even if the service is not 
//  used and there is no valid session id to be provided, this extra has to be present 
//  with a null value to launch a custom tab.

private static final String EXTRA_CUSTOM_TABS_SESSION = "android.support.customtabs.extra.SESSION";
Bundle extras = new Bundle;
extras.putBinder(EXTRA_CUSTOM_TABS_SESSION, 
   sessionICustomTabsCallback.asBinder() /* Set to null for no session */);
intent.putExtras(extras);

Thêm các tuỳ chỉnh giao diện người dùng

Tuỳ chỉnh giao diện người dùng được bao gồm bằng cách thêm Tuỳ chọn khác vào Ý định ACTION_VIEW. Bạn có thể xem danh sách đầy đủ các khoá bổ sung dùng để tuỳ chỉnh giao diện người dùng trên tài liệu về CustomTabsIntent. Sau đây là ví dụ về cách thêm màu sắc tuỳ chỉnh cho thanh công cụ:

// Extra that changes the background color for the address bar. colorInt is an int
// that specifies a Color.

private static final String EXTRA_CUSTOM_TABS_TOOLBAR_COLOR = "android.support.customtabs.extra.TOOLBAR_COLOR";
intent.putExtra(EXTRA_CUSTOM_TABS_TOOLBAR_COLOR, colorInt);

Kết nối với dịch vụ Thẻ tuỳ chỉnh

Bạn có thể sử dụng dịch vụ Thẻ tuỳ chỉnh theo cách tương tự như các Dịch vụ Android khác. Giao diện này được tạo bằng AIDL và tự động tạo một lớp dịch vụ proxy cho bạn.

Sử dụng các phương pháp trên dịch vụ proxy để khởi động, tạo phiên và tìm nạp trước:

// Package name for the Chrome channel the client wants to connect to. This
// depends on the channel name.
// Stable = com.android.chrome
// Beta = com.chrome.beta
// Dev = com.chrome.dev
public static final String CUSTOM_TAB_PACKAGE_NAME = "com.chrome.dev";  // Change when in stable

// Action to add to the service intent. This action can be used as a way 
// generically pick apps that handle custom tabs for both activity and service 
// side implementations.
public static final String ACTION_CUSTOM_TABS_CONNECTION =
       "android.support.customtabs.action.CustomTabsService";
Intent serviceIntent = new Intent(ACTION_CUSTOM_TABS_CONNECTION);

serviceIntent.setPackage(CUSTOM_TAB_PACKAGE_NAME);
context.bindService(serviceIntent, mServiceConnection,
                    Context.BIND_AUTO_CREATE | Context.BIND_WAIVE_PRIORITY);