استفاده از Custom Tab سطح پایین API

روش پیشنهادی برای ادغام برنامه خود با تب های سفارشی استفاده از کتابخانه مرورگر AndroidX است، اما می توانید یک تب سفارشی را بدون کتابخانه پشتیبانی نیز راه اندازی کنید. این راهنما یک نمای کلی در مورد چگونگی دستیابی به این امر ارائه می دهد.

اجرای کامل کتابخانه پشتیبانی در GitHub در دسترس است و می تواند به عنوان نقطه شروع استفاده شود. همچنین حاوی فایل‌های AIDL مورد نیاز برای اتصال به سرویس است، زیرا فایل‌های موجود در مخزن Chromium مستقیماً با Android Studio قابل استفاده نیستند.

اصول راه اندازی برگه های سفارشی با استفاده از 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 Intent، سفارشی‌سازی‌های رابط کاربری شامل می‌شوند. فهرست کاملی از کلیدهای اضافی مورد استفاده برای سفارشی کردن رابط کاربری را می توان در اسناد 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);

اتصال به سرویس Custom Tabs

از سرویس تب های سفارشی می توان به همان روشی که سایر سرویس های اندروید استفاده می شود استفاده کرد. رابط کاربری با 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);