La méthode recommandée pour intégrer votre application aux onglets personnalisés consiste à utiliser la bibliothèque AndroidX Browser. Toutefois, vous pouvez également démarrer un onglet personnalisé sans la bibliothèque Support. Ce guide vous explique comment y parvenir.
L'implémentation complète de la bibliothèque Support est disponible sur [GitHub][1] et peut être utilisée comme point de départ. Il contient également les [fichiers AIDL][2] nécessaires pour se connecter au service, car ceux contenus dans le dépôt Chromium ne sont pas directement utilisables avec Android Studio.
Principes de base pour lancer des onglets personnalisés à l'aide de l'API de bas niveau
// 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);
Ajouter des personnalisations de l'UI
Les personnalisations de l'UI sont incluses en ajoutant des extras à l'intent ACTION_VIEW. Vous trouverez la liste complète des clés supplémentaires utilisées pour personnaliser l'UI dans la [documentation CustomTabsIntent][3]. Voici un exemple d'ajout d'une couleur personnalisée à la barre d'outils :
// 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);
Se connecter au service d'onglets personnalisés
Le service d'onglets personnalisés peut être utilisé de la même manière que les autres services Android. L'interface est créée avec AIDL et crée automatiquement une classe de service proxy pour vous.
Utilisez les méthodes du service proxy pour préchauffer, créer des sessions et précharger :
// 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);
[1]: https://github.com/GoogleChrome/custom-tabs-client/tree/master/customtabs [2]: https://developer.android.com/guide/components/aidl.html [3]: https://developer.android.com/reference/androidx/browser/customtabs/CustomTabsIntent