Cómo usar la API de bajo nivel de Custom Tab

La forma recomendada de integrar tu aplicación con pestañas personalizadas es usar la biblioteca del navegador AndroidX, pero también puedes iniciar una pestaña personalizada sin la biblioteca de compatibilidad. En esta guía, se ofrece una descripción general para lograrlo.

La implementación completa de la biblioteca de compatibilidad está disponible en GitHub y se puede usar como punto de partida. Además, contiene los archivos AIDL necesarios para conectarse al servicio, ya que los que se encuentran en el repositorio de Chromium no se pueden usar directamente con Android Studio.

Conceptos básicos para iniciar pestañas personalizadas con la API de bajo nivel

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

Cómo agregar personalizaciones de IU

Las personalizaciones de la IU se incluyen agregando elementos adicionales al intent ACTION_VIEW. Puedes encontrar una lista completa de las teclas adicionales que se usan para personalizar la IU en los documentos de CustomTabsIntent. A continuación, se muestra un ejemplo de cómo agregar un color personalizado a la barra de herramientas:

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

Conéctate al servicio de pestañas personalizadas

El servicio de pestañas personalizadas se puede usar de la misma manera que otros servicios de Android. La interfaz se crea con AIDL y crea automáticamente una clase de servicio de proxy.

Utiliza los métodos del servicio de proxy para realizar la preparación, crear sesiones y realizar una recuperación previa:

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