自 2025 年 3 月 27 日起,我們建議您使用 android-latest-release
而非 aosp-main
建構及貢獻 AOSP。詳情請參閱「Android 開放原始碼計畫變更」。
建立 AppCard
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
如要建立 AppCard,應用程式必須在資訊清單中建立擴充 AppCardContentProvider
的供應商。AppCardContentProvider
會抽象化底層詳細資料,方便建立 AppCard。
資訊清單宣告
如要建立 AppCard,應用程式必須在資訊清單中建立供應商,以擴充 AppCardContentProvider
。
<provider android:name=".SimpleAppCardContentProvider"
android:authorities="com.example.appcard.sample.media"
android:permission="@string/host_permission"
android:exported="true"
android:enabled="true">
<intent-filter>
<action android:name="com.android.car.appcard.APP_CARD_PROVIDER" />
</intent-filter>
</provider>
每個套件只能定義一個供應商,且必須具備下列屬性:
android:exported="true"
android:enabled="true"
android:permission="@string/host_permission"
或
@string/host_permission
存在於 AppCard 程式庫中,並根據系統的 Android API 版本定義權限。
只有使用 Gradle 建構時,才能使用字串資源。使用 Soong 時,請根據適當的資源限定符,以字串資源值指定明確的字串。
(預設) android:grantUriPermissions="false"
(預設) android:forceUriPermissions="false"
為避免出現非預期的結果,供應商必須在 android:authorities
中只定義單一授權。
宣告以動作為依據的意圖篩選器:
com.android.car.appcard.APP_CARD_PROVIDER
擴充 AppCardContentProvider
本節說明 override 和 protected 最終方法。
覆寫方法
val authority: String
使用這個方法傳回 android:authorities
資訊清單屬性中定義的授權。
fun onCreate(): Boolean
這個方法必須呼叫 super.onCreate()
。使用這個方法設定功能時,如果是在要求 AppCard 時設定,可能會導致延遲。
val appCardIds: List
使用這個方法傳回支援的 AppCard ID 清單。建議您讓每個 ID 盡可能詳細,因為這個字串會用於記錄錯誤。
fun onAppCardAdded(String, AppCardContext): AppCard
:首次顯示 AppCard 時,系統會呼叫這個方法,並提供與 AppCard 相關的 ID 和 AppCardContext
,藉此提供 AppCard 顯示方式的提示。
使用這個方法,設定 AppCards 支援的任何功能 (以供應商身分)。呼叫此函式後,系統會將與指定 ID 相對應的 AppCard 視為「有效」。
fun onAppCardRemoved(String)
當系統未向使用者顯示任何 AppCard 剩餘例項時,就會呼叫這個方法,並處理所有清除作業。呼叫這個函式時,系統會將對應指定 ID 的 AppCard 視為非使用中。
fun onAppCardContextChanged(String, AppCardContext)
當系統要更新 AppCard 的顯示方式時,就會呼叫這個方法,並傳送更新後的 AppCardContext
。
受保護的最終方法
fun sendAppCardUpdate(AppCard)
呼叫這個方法,即可將有效 AppCard 的更新排入佇列。
fun sendAppCardComponentUpdate(String, Component)
在有效的 AppCard 中,呼叫這個方法即可將元件更新排入佇列。如果指定元件標記為 EnforceFastUpdateRate
,系統會立即傳送更新。
常見問題
範例實作位置
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-09-05 (世界標準時間)。
[null,null,["上次更新時間:2025-09-05 (世界標準時間)。"],[],[],null,["To create an AppCard, an app must create a provider in the manifest that extends\n`AppCardContentProvider`. The `AppCardContentProvider` abstracts away underlying\ndetails to facilitate the creation of AppCards.\n\nManifest declaration\n\nTo create an AppCard, an app must create a provider in the manifest to extend\n`AppCardContentProvider`. \n\n \u003cprovider android:name=\".SimpleAppCardContentProvider\"\n android:authorities=\"com.example.appcard.sample.media\"\n android:permission=\"@string/host_permission\"\n android:exported=\"true\"\n android:enabled=\"true\"\u003e\n \u003cintent-filter\u003e\n \u003caction android:name=\"com.android.car.appcard.APP_CARD_PROVIDER\" /\u003e\n \u003c/intent-filter\u003e\n \u003c/provider\u003e\n\n**Only one provider can be defined per package** and with these properties:\n\n- `android:exported=\"true\"`\n- `android:enabled=\"true\"`\n- `android:permission=\"@string/host_permission\"`\n\n OR,\n - `android:readPermission=\"@string/host_permission\"`\n\n AND,\n\n `android:writePermission=\"@string/host_permission\"`\n- `@string/host_permission` exists in the AppCard library and defines a\n permission depending on the Android API version of the system.\n\n Using the string resource works only when building with Gradle. When using\n Soong, specify the explicit string with the string resource value according\n to the appropriate resource qualifier.\n- (*default* ) `android:grantUriPermissions=\"false\"`\n\n- (*default* ) `android:forceUriPermissions=\"false\"`\n\n To avoid unexpected results, it's required that the provider define a single\n authority **only** in `android:authorities`.\n- Declare an action-based intent filter,\n `com.android.car.appcard.APP_CARD_PROVIDER`\n\nExtend AppCardContentProvider\n\nThis section describes **override** and **protected** final methods.\n\nOverride methods\n\n\u003cbr /\u003e\n\n`val authority: String` Use this method to return the authority defined in the `android:authorities` manifest property.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n`fun onCreate(): Boolean` This method must call `super.onCreate()`. Use this method to set up functionality that could potentially cause a delay if set up when an AppCard is requested.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n`val appCardIds: List` Use this method to return the list of supported AppCard IDs. We recommend making each ID verbose since this string is used to log errors.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n`fun onAppCardAdded(String, AppCardContext): AppCard` This method is called when an AppCard is shown for the first time and provides the ID related to the AppCard and the `AppCardContext` that provide hints as to how the AppCard is displayed.\n\n\u003cbr /\u003e\n\nUse this method to set up any functionality required by the AppCards\nsupported as a provider. Once this function is called, the AppCard that\ncorresponds to the given ID is considered to be **active.**\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n`fun onAppCardRemoved(String)` This method is called when no remaining instances of the AppCard are shown to the user and handles all clean up. When this function is called, the AppCard that corresponds to the given ID is considered **inactive.**\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n`fun onAppCardContextChanged(String, AppCardContext)` This method is called when the system wants to update how an AppCard is displayed and sends an updated `AppCardContext`.\n\n\u003cbr /\u003e\n\nProtected final methods\n\n\u003cbr /\u003e\n\n`fun sendAppCardUpdate(AppCard)` Invoke this method to queue an update for an active AppCard.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n`fun sendAppCardComponentUpdate(String, Component)` Invoke this method to queue an update for a component in an active AppCard. If the given component is tagged with `EnforceFastUpdateRate`, then the update is sent immediately.\n\n\u003cbr /\u003e\n\nFAQ\n\nWhere are the sample implementations?\n\n- [Calendar AppCard](https://android.googlesource.com/platform/packages/apps/Car/libs/+/refs/tags/ub-automotive-master-20250418/car-app-card-lib/sample-calendar-app/)\n- [Media AppCard](https://android.googlesource.com/platform/packages/apps/Car/libs/+/refs/tags/ub-automotive-master-20250418/car-app-card-lib/sample-media-app/)\n- [Weather AppCard](https://android.googlesource.com/platform/packages/apps/Car/libs/+/refs/tags/ub-automotive-master-20250418/car-app-card-lib/sample-weather-app/)"]]