Kể từ ngày 27 tháng 3 năm 2025, bạn nên sử dụng android-latest-release
thay vì aosp-main
để xây dựng và đóng góp cho AOSP. Để biết thêm thông tin, hãy xem phần Thay đổi đối với AOSP.
Tạo AppCard
Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Để tạo AppCard, ứng dụng phải tạo một trình cung cấp trong tệp kê khai mở rộng AppCardContentProvider
. AppCardContentProvider
sẽ trừu tượng hoá các thông tin chi tiết cơ bản để tạo điều kiện thuận lợi cho việc tạo AppCard.
Nội dung khai báo trong tệp kê khai
Để tạo AppCard, ứng dụng phải tạo một trình cung cấp trong tệp kê khai để mở rộng 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>
Bạn chỉ có thể xác định một nhà cung cấp cho mỗi gói và có các thuộc tính sau:
android:exported="true"
android:enabled="true"
android:permission="@string/host_permission"
HOẶC,
@string/host_permission
có trong thư viện AppCard và xác định một quyền tuỳ thuộc vào phiên bản API Android của hệ thống.
Việc sử dụng tài nguyên chuỗi chỉ hoạt động khi bạn tạo bằng Gradle. Khi sử dụng Soong, hãy chỉ định chuỗi rõ ràng bằng giá trị tài nguyên chuỗi theo trình đủ tiêu chuẩn tài nguyên thích hợp.
(mặc định) android:grantUriPermissions="false"
(mặc định) android:forceUriPermissions="false"
Để tránh kết quả không mong muốn, nhà cung cấp phải xác định chỉ một cơ quan duy nhất trong android:authorities
.
Khai báo bộ lọc ý định dựa trên hành động, com.android.car.appcard.APP_CARD_PROVIDER
Mở rộng AppCardContentProvider
Phần này mô tả các phương thức cuối cùng ghi đè và được bảo vệ.
Phương thức ghi đè
val authority: String
Sử dụng phương thức này để trả về quyền được xác định trong thuộc tính tệp kê khai android:authorities
.
fun onCreate(): Boolean
Phương thức này phải gọi super.onCreate()
. Sử dụng phương thức này để thiết lập chức năng có thể gây ra độ trễ nếu được thiết lập khi AppCard được yêu cầu.
val appCardIds: List
Sử dụng phương thức này để trả về danh sách mã nhận dạng AppCard được hỗ trợ. Bạn nên đặt mỗi mã nhận dạng ở chế độ chi tiết vì chuỗi này được dùng để ghi lại lỗi.
fun onAppCardAdded(String, AppCardContext): AppCard
Phương thức này được gọi khi AppCard xuất hiện lần đầu tiên và cung cấp mã nhận dạng liên quan đến AppCard cũng như AppCardContext
cung cấp gợi ý về cách AppCard xuất hiện.
Sử dụng phương thức này để thiết lập mọi chức năng mà AppCards yêu cầu được hỗ trợ dưới dạng nhà cung cấp. Sau khi hàm này được gọi, AppCard tương ứng với mã nhận dạng đã cho sẽ được coi là đang hoạt động.
fun onAppCardRemoved(String)
Phương thức này được gọi khi không còn phiên bản AppCard nào hiển thị cho người dùng và xử lý mọi hoạt động dọn dẹp. Khi hàm này được gọi, AppCard tương ứng với mã nhận dạng đã cho sẽ được coi là không hoạt động.
fun onAppCardContextChanged(String, AppCardContext)
Phương thức này được gọi khi hệ thống muốn cập nhật cách hiển thị AppCard và gửi một AppCardContext
đã cập nhật.
Phương thức cuối cùng được bảo vệ
fun sendAppCardUpdate(AppCard)
Gọi phương thức này để đưa một bản cập nhật vào hàng đợi cho AppCard đang hoạt động.
fun sendAppCardComponentUpdate(String, Component)
Gọi phương thức này để đưa một yêu cầu cập nhật vào hàng đợi cho một thành phần trong AppCard đang hoạt động. Nếu thành phần đã cho được gắn thẻ bằng EnforceFastUpdateRate
, thì bản cập nhật sẽ được gửi ngay lập tức.
Câu hỏi thường gặp
Các phương thức triển khai mẫu nằm ở đâu?
Nội dung và mã mẫu trên trang này phải tuân thủ các giấy phép như mô tả trong phần Giấy phép nội dung. Java và OpenJDK là nhãn hiệu hoặc nhãn hiệu đã đăng ký của Oracle và/hoặc đơn vị liên kết của Oracle.
Cập nhật lần gần đây nhất: 2025-09-05 UTC.
[[["Dễ hiểu","easyToUnderstand","thumb-up"],["Giúp tôi giải quyết được vấn đề","solvedMyProblem","thumb-up"],["Khác","otherUp","thumb-up"]],[["Thiếu thông tin tôi cần","missingTheInformationINeed","thumb-down"],["Quá phức tạp/quá nhiều bước","tooComplicatedTooManySteps","thumb-down"],["Đã lỗi thời","outOfDate","thumb-down"],["Vấn đề về bản dịch","translationIssue","thumb-down"],["Vấn đề về mẫu/mã","samplesCodeIssue","thumb-down"],["Khác","otherDown","thumb-down"]],["Cập nhật lần gần đây nhất: 2025-09-05 UTC."],[],[],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/)"]]