Utilizzo dell'API di basso livello della Scheda personalizzata

Il modo consigliato per integrare l'applicazione con le schede personalizzate è utilizzare la libreria AndroidX Browser, ma puoi anche avviare una scheda personalizzata senza la libreria di supporto. Questa guida fornisce una panoramica su come raggiungere questo obiettivo.

L'implementazione completa della Support Library è disponibile su [GitHub][1] e può essere utilizzata come punto di partenza. Contiene anche i [file AIDL][2] necessari per connettersi al servizio, poiché quelli contenuti nel repository Chromium non sono direttamente utilizzabili con Android Studio.

Nozioni di base per l'avvio di schede personalizzate utilizzando l'API di basso livello

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

Aggiunta di personalizzazioni della UI

Le personalizzazioni dell'interfaccia utente vengono incluse aggiungendo extra all'intent ACTION_VIEW. Un elenco completo delle chiavi extra utilizzate per personalizzare la UI è disponibile nella [documentazione di CustomTabsIntent][3]. Di seguito è riportato un esempio di come aggiungere un colore personalizzato alla barra degli strumenti:

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

Connessione al servizio Schede personalizzate

Il servizio Schede personalizzate può essere utilizzato allo stesso modo degli altri servizi Android. L'interfaccia viene creata con AIDL e genera automaticamente una classe di servizio proxy.

Utilizza i metodi del servizio proxy per il riscaldamento, la creazione di sessioni e il precaricamento:

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