ตรวจสอบว่าอุปกรณ์ Android มีเบราว์เซอร์ที่รองรับแท็บที่กำหนดเองหรือไม่

หากต้องการทราบว่าเบราว์เซอร์เริ่มต้นหรือเบราว์เซอร์ในอุปกรณ์รองรับแท็บที่กำหนดเองหรือไม่ ให้ใช้ตัวช่วยของ getPackageName ใน CustomTabsClient ดังนี้

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

และคุณยังตรวจสอบได้ว่ามีเบราว์เซอร์ใดในอุปกรณ์ที่รองรับแท็บที่กำหนดเองหรือไม่ โดยทำดังนี้

// 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 ได้เปิดตัวการเปลี่ยนแปลงระดับการเข้าถึงแพ็กเกจ หากแอป Android ของคุณกำหนดเป้าหมายเป็น API ระดับ 30 ขึ้นไป คุณจำเป็นต้องเพิ่มส่วนคำค้นหาต่อไปนี้ลงใน AndroidManifest.xml มิฉะนั้นข้อมูลโค้ดด้านบนจะไม่แสดงผลลัพธ์

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