Kiểm tra xem thiết bị Android có trình duyệt hỗ trợ Thẻ tuỳ chỉnh hay không?

Nếu bạn muốn biết liệu trình duyệt mặc định hay trình duyệt bất kỳ trên thiết bị có hỗ trợ Thẻ tuỳ chỉnh hay không, hãy sử dụng trình trợ giúp getPackageName trong CustomTabsClient:

String packageName = CustomTabsClient.getPackageName(
        context, 
        Collections.emptyList()
);
if (packageName == null) {
    // Custom Tabs are not supported by the default browser
}

Bạn cũng có thể kiểm tra xem có trình duyệt nào trên thiết bị hỗ trợ Thẻ tuỳ chỉnh hay không:

// 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> viewIntentHandlers = packageManager.queryIntentActivities(activityIntent, 0);
// Get a package that supports Custom Tabs
String packageName = CustomTabsClient.getPackageName(
        context, 
        viewIntentHandlers,
        true /* ignore default */
);
if (packageName == null) {
    // Custom Tabs are not supported by any browser on the device
}

Android 11 đã ra mắt các thay đổi về chế độ hiển thị gói. Nếu ứng dụng Android của bạn đang nhắm đến API cấp 30 trở lên, thì bạn cần thêm phần truy vấn sau vào AndroidManifest.xml. Nếu không, đoạn mã ở trên sẽ không trả về kết quả:

<queries>
    <intent>
        <action android:name=
            "android.support.customtabs.action.CustomTabsService" />
    </intent>
</queries>