Met behulp van de aangepaste tabblad-API op laag niveau

De aanbevolen manier om uw applicatie te integreren met aangepaste tabbladen is het gebruik van de AndroidX-browserbibliotheek, maar u kunt ook een aangepast tabblad starten zonder de ondersteuningsbibliotheek. Deze handleiding geeft een overzicht van hoe u dit kunt bereiken.

De volledige implementatie van de Support Library is beschikbaar op GitHub en kan als uitgangspunt worden gebruikt. Het bevat ook de AIDL-bestanden die nodig zijn om verbinding te maken met de service, aangezien de bestanden in de Chromium-repository niet direct bruikbaar zijn met Android Studio.

Basisprincipes voor het starten van aangepaste tabbladen met behulp van de 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);

UI-aanpassingen toevoegen

UI-aanpassingen zijn inbegrepen door extra's toe te voegen aan de ACTION_VIEW-intentie. Een volledige lijst met de extra toetsen die worden gebruikt om de gebruikersinterface aan te passen, kunt u vinden in de CustomTabsIntent-documenten . Hieronder volgt een voorbeeld van hoe u een aangepaste werkbalkkleur kunt toevoegen:

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

Verbinding maken met de Custom Tabs-service

De Custom Tabs-service kan op dezelfde manier worden gebruikt als andere Android-services. De interface is gemaakt met AIDL en creëert automatisch een proxyserviceklasse voor u.

Gebruik de methoden op de proxyservice om op te warmen, sessies te maken en vooraf op te halen:

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