The following sections show you how to confirm weather the default browser or any browser on an Android device supports Custom Tabs.
Check if the default browser support Custom Tabs
To verify if the default browser supports Custom Tabs, use the
getPackageName
helper method in CustomTabsClient
:
String packageName = CustomTabsClient.getPackageName(
context,
Collections.emptyList()
);
if (packageName == null) {
// Custom Tabs are not supported by the default browser
}
Check if any browser on the device supports Custom Tabs
To verify if any browser installed on the device supports Custom Tabs, you need
to query for apps that can handle VIEW
intents, extract their package
names, and use the
getPackageName
helper method in CustomTabsClient
:
// Get all apps that can handle VIEW intents and Custom Tab service connections.
Intent activityIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.example.com"));
PackageManager packageManager = context.getPackageManager();
List<ResolveInfo> resolveInfos = packageManager.queryIntentActivities(activityIntent, PackageManager.MATCH_ALL);
// Extract package names from ResolveInfo objects
List<String> packageNames = new ArrayList<>();
for (ResolveInfo info : resolveInfos) {
packageNames.add(info.activityInfo.packageName);
}
// Get a package that supports Custom Tabs
String packageName = CustomTabsClient.getPackageName(
context,
packageNames,
true /* ignore default */
);
if (packageName == null) {
// Custom Tabs are not supported by any browser on the device
}
Android 11 package visibility changes
Android 11 has introduced package visibility changes. If your Android app
targets API level 30 or later, you must add the following declaration in the
queries section of the AndroidManifest.xml
.
Without this declaration, the queryIntentActivities
method won't
return results:
<queries>
<intent>
<action android:name=
"android.support.customtabs.action.CustomTabsService" />
</intent>
</queries>
No browser in device supports Custom Tabs
If no browser supporting Custom Tabs is available in the device and you
launch a URL using customTabsIntent.launchUrl(context, url)
, the intent might
fail, leading to an ActivityNotFoundException
.
Always perform a compatibility check to ensure a better user experience.
You can fallback to a standard ACTION_VIEW
Intent to open the URL in any available browser.