שימוש ב-API ברמה נמוכה של הכרטיסייה המותאמת אישית

הדרך המומלצת לשילוב האפליקציה עם 'כרטיסיות מותאמות אישית' היא באמצעות ספריית הדפדפן של AndroidX, אבל אפשר גם להפעיל כרטיסייה בהתאמה אישית בלי ספריית התמיכה. המדריך הזה מספק סקירה כללית לגבי האופן שבו ניתן לעשות זאת.

ההטמעה המלאה של ספריית התמיכה זמינה ב-GitHub ואפשר להשתמש בה לנקודת ההתחלה. הוא מכיל גם את קובצי ה-AIDL שנדרשים כדי להתחבר לשירות, כמו הקבצים שבמאגר Chromium אי אפשר להשתמש ישירות ב-Android Studio.

עקרונות בסיסיים להפעלת כרטיסיות מותאמות אישית באמצעות Low Level API

// 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);

הוספת התאמות אישיות של ממשק המשתמש

כדי לכלול התאמות אישיות של ממשק המשתמש, מוסיפים תוספות ל-Intent ACTION_VIEW. רשימה מלאה של אפשר למצוא מפתחות נוספים שמשמשים להתאמה אישית של ממשק המשתמש במסמכי CustomTabsIntent. דוגמה בנושא איך להוסיף צבעים מותאמים אישית של סרגל הכלים:

// 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);

התחברות לשירות 'כרטיסיות מותאמות אישית'

אפשר להשתמש בשירות 'כרטיסיות מותאמות אישית' באותו אופן שבו משתמשים בשירותים אחרים של Android. הממשק שנוצר באמצעות AIDL ויוצר אוטומטית סיווג שירות של שרת proxy.

משתמשים ב-methods בשירות ה-Proxy כדי לבצע חימום, ליצור סשנים ולאחזר מראש:

// 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);