Custom Tab 하위 수준 API 사용

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

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

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

UI 맞춤설정은 ACTION_VIEW 인텐트에 Extras를 추가하여 포함됩니다. 전체 목록은 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 서비스와 동일한 방식으로 사용할 수 있습니다. 인터페이스는 프록시 서비스 클래스를 자동으로 생성합니다.

프록시 서비스의 메서드를 사용하여 세션을 준비하고, 세션을 만들고, 미리 가져옵니다.

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