ตั้งแต่วันที่ 27 มีนาคม 2025 เป็นต้นไป เราขอแนะนำให้ใช้ android-latest-release
แทน aosp-main
เพื่อสร้างและมีส่วนร่วมใน AOSP โปรดดูข้อมูลเพิ่มเติมที่หัวข้อการเปลี่ยนแปลงใน AOSP
กำหนดค่าโฮสต์ AppCard
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
ใช้ AppCardHost
เพื่อสื่อสารกับแอปที่แสดง AppCard
สิทธิ์
แอปที่ใช้ AppCardHost
ต้องมีสิทธิ์ต่อไปนี้
android.permission.INTERACT_ACROSS_USERS_FULL
android.permission.QUERY_ALL_PACKAGES
- (ใน API ระดับ 34 ขึ้นไปเท่านั้น)
android.car.permission.BIND_APP_CARD_PROVIDER
เริ่มต้น
หากต้องการเริ่มต้นใช้งานโฮสต์ ให้ระบุข้อมูลต่อไปนี้
- บริบท
updateRate
อัตราการอัปเดต AppCard เป็นมิลลิวินาที
fastUpdateRate
อัตราการอัปเดตเป็นมิลลิวินาทีสําหรับคอมโพเนนต์ที่ติดแท็กด้วย
EnforceFastUpdateRate
responseExecutor
ชุดข้อความที่คุณต้องการรับการตอบกลับจากแอป AppCard
การออกแบบ
คุณสามารถโต้ตอบกับ AppCardHost
ได้ 2 วิธีดังนี้
หรือ
AppCardListener
คอมโพเนนต์ AppCardListener
จะลงทะเบียนกับ AppCardHost
เพื่อรับการอัปเดตการสื่อสารจากแอป AppCardHost
และแอป AppCard
ดูรายละเอียดฟังก์ชันอินเทอร์เฟซของ AppCardListener
ได้ที่นี่
fun onAppCardReceived(AppCardContainer)
ฟังก์ชันนี้จะทริกเกอร์เมื่อได้รับ AppCard จากแอป โดยจะส่งเป็น AppCardContainer
ซึ่งระบุ AppCard และ appIdentifier
เพื่อระบุแอปที่ส่ง AppCard
fun onComponentReceived(AppCardComponentContainer)
ฟังก์ชันนี้จะทริกเกอร์เมื่อได้รับคอมโพเนนต์ AppCard จากแอป โดยระบบจะส่งเป็น AppCardComponentContainer
ซึ่งให้คอมโพเนนต์ AppCard, Identifier
เพื่อระบุแอปที่ส่ง AppCard และรหัสสตริงเพื่อชี้ไปยัง AppCard ที่คอมโพเนนต์นั้นเกี่ยวข้อง
fun onProviderRemoved(String, String?)
ฟังก์ชันนี้จะทริกเกอร์เมื่อนำผู้ให้บริการ AppCard ออกหรือปิดใช้ ใช้วิธีนี้เพื่อล้าง AppCard ที่ใช้งานอยู่ซึ่งเกี่ยวข้องกับชื่อแพ็กเกจและสิทธิ์ของผู้ให้บริการที่ระบุ
หากมีสิทธิ์เป็น {@code null}
แสดงว่าระบบนําทั้งแพ็กเกจออกแล้ว
fun onProviderAdded(String, String?)
ฟังก์ชันนี้จะทริกเกอร์เมื่อมีการเพิ่มหรือเปิดใช้ผู้ให้บริการ AppCard
ตัวอย่างการใช้งาน ใช้ฟังก์ชันนี้เป็นทริกเกอร์เพื่อรีเฟรช AppCard ทั้งหมดที่ใช้ได้ในตัวเลือก AppCard หากมีสิทธิ์เป็น {@code null}
แสดงว่ามีการเพิ่มทั้งแพ็กเกจ
fun onPackageCommunicationError(appIdentifier, Throwable)
ฟังก์ชันนี้จะทริกเกอร์เมื่อ AppCardHost
พบข้อผิดพลาดขณะสื่อสารกับผู้ให้บริการ AppCard
ตัวอย่างการใช้งาน ใช้วิธีการนี้เพื่อแสดงให้ผู้ใช้ทราบว่า AppCard ที่เลือกพบข้อผิดพลาด
API
fun refreshCompatibleapp()
ควรเรียกใช้เมธอดนี้ทุกครั้งที่กิจกรรมที่ใช้โฮสต์กลับมาทำงานอีกครั้งเพื่อให้โฮสต์รีเฟรชรายการแอปที่รองรับ AppCard ได้
fun destroy()
เรียกใช้เมธอดนี้เมื่อกิจกรรมที่ใช้โฮสต์ถูกทำลายเพื่อให้โฮสต์ล้างการเชื่อมต่อและสมาชิกภายในทั้งหมด
fun registerListener(AppCardListener)
ใช้เพื่อลงทะเบียน AppCardListener
fun unregisterListener(AppCardListener)
ใช้ยกเลิกการลงทะเบียน AppCardListener
fun getAllAppCards(AppCardContext)
เรียกใช้เมธอดนี้เพื่อระบุ AppCardListener
ที่ลงทะเบียนไว้พร้อม AppCard ทั้งหมดที่มีให้ในระบบด้วย AppCardContext
ที่ระบุ ซึ่งจะให้คำแนะนำแก่ผู้ให้บริการเกี่ยวกับวิธีจัดโครงสร้าง AppCard
fun requestAppCard(AppCardContext, appIdentifier, String)
เรียกใช้เมธอดนี้เพื่อระบุ AppCard ที่เฉพาะเจาะจงให้กับ AppCardListener
ที่ลงทะเบียน โดยให้ AppCardContext
ที่บอกคำแนะนำแก่ผู้ให้บริการเกี่ยวกับวิธีจัดโครงสร้าง AppCard
fun notifyAppCardRemoved(appIdentifier, String)
แจ้งให้ผู้ให้บริการ AppCard ทราบว่า AppCard ของผู้ให้บริการดังกล่าวไม่ทำงานแล้ว
fun notifyAppCardInteraction(appIdentifier, String, String, String)
แจ้งผู้ให้บริการ AppCard ว่ามีการโต้ตอบกับ AppCard ของผู้ให้บริการ
การโต้ตอบที่รองรับเพียงอย่างเดียวคือการคลิกปุ่ม ซึ่งแสดงด้วย AppCardMessageConstants.InteractionMessageConstants.MSG_INTERACTION_ON_CLICK
เราขอแนะนำให้สร้างปุ่มสำหรับ AppCard ที่มี onClick
Listener ที่เรียกใช้ฟังก์ชันนี้ด้วย appIdentifier
, รหัส AppCard, รหัสคอมโพเนนต์ และรหัสการโต้ตอบ
fun sendAppCardContextUpdate(AppCardContext, appIdentifier, String)
ส่งการอัปเดต AppCardContext
สำหรับ AppCard ที่เฉพาะเจาะจง เช่น เมื่อเปลี่ยนจากโหมดเข้าเกียร์ว่างเป็นโหมดขับเคลื่อน ให้ใช้วิธีนี้เพื่อส่งAppCardContext
การอัปเดตโดยตั้งค่า isInteractable
เป็น false
สําหรับ AppCard ที่ใช้งานอยู่แต่ละรายการ
คำถามที่พบบ่อย
ฉันจะดูตัวอย่างการใช้งานได้จากที่ใด
AppCardContentProvider
แต่ละรายการรองรับ AppCard ได้กี่รายการ
AppCardContentProvider
รองรับ AppCard ได้ไม่จำกัดจำนวน
อย่างไรก็ตาม อย่าลืมรักษาสมดุลระหว่างจำนวน AppCard กับประสิทธิภาพที่ลดลงเทียบกับประสบการณ์การใช้งานที่ดีของผู้ใช้
ตัวอย่างเนื้อหาและโค้ดในหน้าเว็บนี้ขึ้นอยู่กับใบอนุญาตที่อธิบายไว้ในใบอนุญาตการใช้เนื้อหา Java และ OpenJDK เป็นเครื่องหมายการค้าหรือเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-06-26 UTC
[[["เข้าใจง่าย","easyToUnderstand","thumb-up"],["แก้ปัญหาของฉันได้","solvedMyProblem","thumb-up"],["อื่นๆ","otherUp","thumb-up"]],[["ไม่มีข้อมูลที่ฉันต้องการ","missingTheInformationINeed","thumb-down"],["ซับซ้อนเกินไป/มีหลายขั้นตอนมากเกินไป","tooComplicatedTooManySteps","thumb-down"],["ล้าสมัย","outOfDate","thumb-down"],["ปัญหาเกี่ยวกับการแปล","translationIssue","thumb-down"],["ตัวอย่าง/ปัญหาเกี่ยวกับโค้ด","samplesCodeIssue","thumb-down"],["อื่นๆ","otherDown","thumb-down"]],["อัปเดตล่าสุด 2025-06-26 UTC"],[],[],null,["# Configure an AppCard host\n\nUse `AppCardHost` to communicate with apps that show AppCards.\n\nPermissions\n-----------\n\nAny app that uses an `AppCardHost` must have the following permissions:\n\n- `android.permission.INTERACT_ACROSS_USERS_FULL`\n- `android.permission.QUERY_ALL_PACKAGES`\n- (In API level 34 and higher **only** ) `android.car.permission.BIND_APP_CARD_PROVIDER`\n\nInitialize\n----------\n\nTo initialize the host, provide:\n\n- Context\n- `updateRate` AppCard update rate in milliseconds.\n- `fastUpdateRate` Update rate in milliseconds for components tagged with `EnforceFastUpdateRate`\n- `responseExecutor` A thread on which you want to receive responses from AppCard apps.\n\nDesign\n------\n\nAn `AppCardHost` can be interacted with in one of two ways:\n\n- Register as an [`AppCardListener`](#appcardlistener)\n\nOR,\n\n- Interact with the [`AppCardHost` APIs](#api)\n\n### AppCardListener\n\nThe `AppCardListener` component registers itself with an `AppCardHost`\nto receive communication updates from the `AppCardHost` and AppCard apps.\nThe interface functions of an `AppCardListener` are detailed here.\n\n\u003cbr /\u003e\n\n`fun onAppCardReceived(AppCardContainer)`\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\nThis function is triggered when an AppCard has been received from an app. It's sent as an `AppCardContainer` which provides an AppCard and an `appIdentifier` to identify the app that sent the AppCard\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n`fun onComponentReceived(AppCardComponentContainer)`\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\nThis function is triggered when an AppCard component is received from an app. It's sent as an `AppCardComponentContainer`, which provides an AppCards component, `Identifier` to identify the app that sent the AppCard, and a string ID to point to the AppCard to which the component is related.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n`fun onProviderRemoved(String, String?)`\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\nThis function is triggered when an AppCard provider has been removed or disabled. Use this method to clean up any active AppCard related to the given package name and provider authority.\u003cbr /\u003e\n\nIf authority is `{@code null}`, then an entire package was removed.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n`fun onProviderAdded(String, String?)`\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\nThis function is triggered when an AppCard provider has been added or enabled.\u003cbr /\u003e\n\n**Sample usage.** Use this function as a trigger to refresh all available\nAppCards in an AppCard picker. If authority is `{@code null}`, then an entire\npackage was added.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n`fun onPackageCommunicationError(appIdentifier, Throwable)`\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\nThis function is triggered when the `AppCardHost` encounters an error when communicating with an AppCard provider.\n\n\u003cbr /\u003e\n\n**Sample usage.** Use this method to show to the user that an AppCard they have\nselected has encountered an error.\n\n\u003cbr /\u003e\n\n### APIs\n\n\u003cbr /\u003e\n\n`fun refreshCompatibleapp()`\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\nThis method should be called whenever the activity that is using the host is resumed so that the host can refresh its list of apps that support AppCards.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n`fun destroy()`\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\nCall this method when an activity that is using a host is destroyed so that the host can clean up all connections and internal members.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n`fun registerListener(AppCardListener)`\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\nUsed to register an `AppCardListener`.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n`fun unregisterListener(AppCardListener)`\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\nUsed to unregister an `AppCardListener`.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n`fun getAllAppCards(AppCardContext)`\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\nCall this method to supply a registered `AppCardListener` with all the AppCards provided in the system with a given `AppCardContext` that provides hints to the providers on how to structure their AppCard.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n`fun requestAppCard(AppCardContext, appIdentifier, String)`\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\nCall this method to supply a registered `AppCardListener` with a specific AppCard, given an `AppCardContext` that gives hints to the providers on how to structure their AppCard.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n`fun notifyAppCardRemoved(appIdentifier, String)`\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\nNotify an AppCard provider that its AppCard is no longer active.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n`fun notifyAppCardInteraction(appIdentifier, String, String, String)`\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\nNotify an AppCard provider that its AppCard has been interacted with. The only supported interaction is a button click, which is signified by a `AppCardMessageConstants.InteractionMessageConstants.MSG_INTERACTION_ON_CLICK`.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\nWe recommend creating a button for an AppCard with an `onClick` listener that calls this function with `appIdentifier`, AppCard ID, component ID, and interaction ID.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n`fun sendAppCardContextUpdate(AppCardContext, appIdentifier, String)`\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\nSend an `AppCardContext` update for a specific AppCard. For example, when shifting from Park mode to Drive mode, use this method to send an `AppCardContext` update in which `isInteractable` is set to `false` for each active AppCard.\n\n\u003cbr /\u003e\n\nFAQ\n---\n\n1. Where can I find sample implementations?\n\n - [Sample host](https://android.googlesource.com/platform/packages/apps/Car/libs/+/refs/tags/ub-automotive-master-20250418/car-app-card-host-lib/sample-host/). Shows all available AppCards in the system, along with\n testing capabilities.\n\n - DriverUI and [Pano manager](https://cs.android.com/android/platform/superproject/+/android-latest-release:packages/services/Car/car_product/distant_display/apps/CarDistantDisplayPanoManager/;l=1?q=CarDistantDisplayPanoManager&sq=&ss=android%2Fplatform%2Fsuperproject%2Fmain). Pano manager acts as the picker while\n DriverUI acts as the presenter.\n\n2. How many AppCards can each `AppCardContentProvider` support?\n\n An `AppCardContentProvider` can support an infinite number of AppCards.\n However, be sure to balance the number of AppCards with degraded performance\n versus a positive user experience."]]