自 2025 年 3 月 27 日起,我们建议您使用 android-latest-release
而非 aosp-main
构建 AOSP 并为其做出贡献。如需了解详情,请参阅 AOSP 的变更。
创建 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&>quot;
< android:>enabled="tru<e"
intent-filter
action android:>name="co<m.android.car.>a<ppcard.AP>P
_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
中仅定义一个授权。
声明基于操作的 intent 过滤器,com.android.car.appcard.APP_CARD_PROVIDER
扩展 AppCardContentProvider
本部分介绍了 override 和 protected final 方法。
替换方法
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 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-09-04。
[[["易于理解","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"]],["最后更新时间 (UTC):2025-09-04。"],[],[],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&\u003equot;\n \u003c android:\u003eenabled=\"tru\u003ce\"\n intent-filter\n action android:\u003ename=\"co\u003cm.android.car.\u003ea\u003cppcard.AP\u003eP_CARD_PROVIDER\" /\n /intent-filter\n /provider\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/)"]]