Utilizzo dell'API di basso livello della Scheda personalizzata

Il metodo consigliato per integrare la tua applicazione con le schede personalizzate è usare la libreria del browser AndroidX, 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 e può essere utilizzata come punto di partenza. Contiene anche i file AIDL necessari per la connessione al servizio, poiché quelli contenuti nel repository Chromium non sono utilizzabili direttamente con Android Studio.

Nozioni di base per l'avvio di schede personalizzate mediante l'API Low Level

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

Aggiunta di personalizzazioni dell'interfaccia utente

Le personalizzazioni dell'interfaccia utente sono incluse aggiungendo altri elementi all'intent ACTION_VIEW. Un elenco completo delle chiavi aggiuntive utilizzate per personalizzare l'interfaccia utente è disponibile nei documenti CustomTabsIntent. 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.

private static final String EXTRA_CUSTOM_TABS_TOOLBAR_COLOR = "android.support.customtabs.extra.TOOLBAR_COLOR";
intent.putExtra(EXTRA_CUSTOM_TABS_TOOLBAR_COLOR, colorInt);

Connessione al servizio Schede personalizzate

Il servizio Schede personalizzate può essere utilizzato come gli altri servizi Android. L'interfaccia viene creata con AIDL e crea automaticamente una classe di servizio proxy per te.

Utilizza i metodi del servizio proxy per il warmup, 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
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);