कस्टम टैब के लो लेवल एपीआई का इस्तेमाल करना

अपने ऐप्लिकेशन को कस्टम टैब के साथ इंटिग्रेट करने का सबसे सही तरीका, AndroidX Browser Library का इस्तेमाल करना है. हालांकि, सपोर्ट लाइब्रेरी के बिना भी कस्टम टैब शुरू किया जा सकता है. इस गाइड में, ऐसा करने का तरीका बताया गया है.

Support Library को लागू करने की पूरी प्रोसेस [GitHub][1] पर उपलब्ध है. इसका इस्तेमाल शुरुआती बिंदु के तौर पर किया जा सकता है. इसमें सेवा से कनेक्ट करने के लिए ज़रूरी [AIDL फ़ाइलें][2] भी शामिल होती हैं. ऐसा इसलिए, क्योंकि Chromium रिपॉज़िटरी में मौजूद फ़ाइलों का इस्तेमाल सीधे तौर पर Android Studio के साथ नहीं किया जा सकता.

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.
val url = "https://paul.kinlan.me/"
val intent = 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.

val extraCustomTabsSession = "android.support.customtabs.extra.SESSION"
val extras = Bundle()
extras.putBinder(
    extraCustomTabsSession,
    sessionICustomTabsCallback.asBinder() /* Set to null for no session */
)
intent.putExtras(extras)

यूज़र इंटरफ़ेस (यूआई) को पसंद के मुताबिक बनाना

ACTION_VIEW इंटेंट में एक्स्ट्रा जोड़कर, यूज़र इंटरफ़ेस (यूआई) को पसंद के मुताबिक बनाया जा सकता है. यूज़र इंटरफ़ेस (यूआई) को पसंद के मुताबिक बनाने के लिए इस्तेमाल की जाने वाली अतिरिक्त कुंजियों की पूरी सूची, [CustomTabsIntent docs][3] पर देखी जा सकती है. कस्टम टूलबार का रंग जोड़ने का तरीका यहां दिया गया है:

// Extra that changes the background color for the address bar. colorInt is an int
// that specifies a Color.

val extraCustomTabsToolbarColor = "android.support.customtabs.extra.TOOLBAR_COLOR"
intent.putExtra(extraCustomTabsToolbarColor, colorInt)

Custom Tabs सेवा से कनेक्ट किया जा रहा है

Custom Tabs सेवा का इस्तेमाल, 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
val customTabPackageName = "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.
val actionCustomTabsConnection =
    "android.support.customtabs.action.CustomTabsService"
val serviceIntent = Intent(actionCustomTabsConnection)

serviceIntent.setPackage(customTabPackageName)
// serviceConnection is your ServiceConnection implementation. Its
// onServiceConnected() callback receives the IBinder for the Custom Tabs service.
context.bindService(
    serviceIntent,
    serviceConnection,
    Context.BIND_AUTO_CREATE or Context.BIND_WAIVE_PRIORITY
)

[1]: https://github.com/GoogleChrome/custom-tabs-client/tree/master/customtabs [2]: https://developer.android.com/guide/components/aidl.html [3]: https://developer.android.com/reference/androidx/browser/customtabs/CustomTabsIntent