We raden je aan je app te integreren met aangepaste tabbladen via de AndroidX Browser Library, maar je kunt ook een aangepast tabblad starten zonder de ondersteuningsbibliotheek. In deze handleiding vindt u een overzicht van hoe u dit doet.
De volledige implementatie van de Support Library is beschikbaar op [GitHub][1] en kan worden gebruikt als startpunt. Het bevat ook de [AIDL-bestanden][2] die nodig zijn om verbinding te maken met de service, omdat de bestanden in de Chromium-repository niet rechtstreeks kunnen worden gebruikt met Android Studio.
Basisprincipes voor het starten van aangepaste tabbladen met 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.
val url = "https://paul.kinlan.me/"
val intent = 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.
val extraCustomTabsSession = "android.support.customtabs.extra.SESSION"
val extras = Bundle()
extras.putBinder(
extraCustomTabsSession,
sessionICustomTabsCallback.asBinder() /* Set to null for no session */
)
intent.putExtras(extras)
UI-aanpassingen toevoegen
UI-aanpassingen worden opgenomen door extra's toe te voegen aan de ACTION_VIEW-intentie. Je vindt een volledige lijst met de extra sleutels die worden gebruikt om de UI aan te passen in de [documentatie over CustomTabsIntent][3]. Hieronder staat een voorbeeld van hoe je een aangepaste werkbalkkleur toevoegt:
// Extra that changes the background color for the address bar. colorInt is an int
// that specifies a Color.
val extraCustomTabsToolbarColor = "android.support.customtabs.extra.TOOLBAR_COLOR"
intent.putExtra(extraCustomTabsToolbarColor, colorInt)
Verbinding maken met de service voor aangepaste tabbladen
De service Aangepaste tabbladen kan op dezelfde manier worden gebruikt als andere Android-services. De interface wordt gemaakt met AIDL en maakt automatisch een proxyserviceklasse voor je.
Gebruik de methoden in 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
val customTabPackageName = "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.
val actionCustomTabsConnection =
"android.support.customtabs.action.CustomTabsService"
val serviceIntent = Intent(actionCustomTabsConnection)
serviceIntent.setPackage(customTabPackageName)
// serviceConnection is your ServiceConnection implementation. Its
// onServiceConnected() callback receives the IBinder for the Custom Tabs service.
context.bindService(
serviceIntent,
serviceConnection,
Context.BIND_AUTO_CREATE or 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