Custom Tab 하위 수준 API 사용

애플리케이션을 맞춤 탭과 통합할 때 권장되는 방법은 AndroidX 브라우저 라이브러리를 사용하는 것이지만, 지원 라이브러리 없이도 맞춤 탭을 시작할 수도 있습니다. 이 가이드에서는 이를 달성하는 방법을 간략하게 설명합니다.

지원 라이브러리의 전체 구현은 GitHub에서 제공되며 시작점으로 사용할 수 있습니다. Chromium 저장소에 포함된 파일은 Android 스튜디오에서 직접 사용할 수 없으므로 서비스에 연결하는 데 필요한 AIDL 파일도 포함되어 있습니다.

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 맞춤설정 추가

ACTION_VIEW 인텐트에 Extras를 추가하는 방식으로 UI 맞춤설정이 포함됩니다. UI를 맞춤설정하는 데 사용되는 추가 키의 전체 목록은 CustomTabsIntent 문서에서 확인할 수 있습니다. 맞춤 툴바 색상을 추가하는 방법의 예는 다음과 같습니다.

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

맞춤 탭 서비스에 연결

맞춤 탭 서비스는 다른 Android 서비스와 동일한 방식으로 사용할 수 있습니다. 인터페이스는 AIDL로 생성되고 프록시 서비스 클래스가 자동으로 생성됩니다.

프록시 서비스의 메서드를 사용하여 준비, 세션 생성, 프리패치를 수행합니다.

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