建構物聯網應用程式
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
有了 IoT 應用程式,使用者在車內即可下達指令,針對已連結的裝置進行相關操作。例如控制特定裝置的狀態:開啟車庫門、切換住家燈具開關或開啟住家保全系統。
在資訊清單中宣告類別支援
您的應用程式必須在 CarAppService
的意圖篩選器中宣告 androidx.car.app.category.IOT
車用應用程式類別:
<application>
...
<service
...
android:name=".MyCarAppService"
android:exported="true">
<intent-filter>
<action android:name="androidx.car.app.CarAppService" />
<category android:name="androidx.car.app.category.IOT"/>
</intent-filter>
</service>
...
<application>
實作應用程式的功能
如要實作您的應用程式,請參閱「使用車輛專用 Android App Library」,以瞭解如何建構 Car App Library 應用程式。此外,由於我們會根據這些指南審查您的應用程式,因此請務必熟悉「IoT 應用程式的車用應用程式品質指南」。
對於 IoT 應用程式,GridTemplate
很適合用來顯示裝置清單,並允許使用者與其進行互動,如以下範例所示:
Kotlin
val listBuilder = ItemList.Builder()
listBuilder.addItem(
GridItem.Builder()
.setTitle("Garage door")
.setImage(...)
// Handle user interactions
.setOnClickListener {...}
.build()
)
listBuilder.addItem(
GridItem.Builder()
.setTitle("Garage lights")
// Show a loading indicator until the status of the device is known
// (call invalidate() when the status is known to refresh the screen)
.setLoading(true)
.build()
)
return GridTemplate.Builder()
.setTitle("Devices")
.setHeaderAction(Action.APP_ICON)
.setSingleList(listBuilder.build())
.build()
Java
ItemList.Builder listBuilder = new ItemList.Builder();
listBuilder.addItem(
new GridItem.Builder()
.setTitle("Garage door")
.setImage(...)
// Handle user interactions
.setOnClickListener(() -> {...})
.build()
);
listBuilder.addItem(
new GridItem.Builder()
.setTitle("Garage lights")
// Show a loading indicator until the status of the device is known
// (call invalidate() when the status is known to refresh the screen)
.setLoading(true)
.build()
);
return new GridTemplate.Builder()
.setTitle("Devices")
.setHeaderAction(Action.APP_ICON)
.setSingleList(listBuilder.build())
.build();
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-07-27 (世界標準時間)。
[null,null,["上次更新時間:2025-07-27 (世界標準時間)。"],[],[],null,["# Build an internet of things app\n\nIOT apps enable users to take relevant actions on connected devices from within\nthe car. Examples include controlling the state of certain devices, such as\nopening a garage door, flipping home light switches, or enabling home security.\n\nDeclare category support in your manifest\n-----------------------------------------\n\nYour app needs to declare the `androidx.car.app.category.IOT`\n[car app category](/training/cars/apps#supported-app-categories) in the intent\nfilter of its [`CarAppService`](/reference/androidx/car/app/CarAppService). \n\n \u003capplication\u003e\n ...\n \u003cservice\n ...\n android:name=\".MyCarAppService\"\n android:exported=\"true\"\u003e\n \u003cintent-filter\u003e\n \u003caction android:name=\"androidx.car.app.CarAppService\" /\u003e\n \u003ccategory android:name=\"androidx.car.app.category.IOT\"/\u003e\n \u003c/intent-filter\u003e\n \u003c/service\u003e\n ...\n \u003capplication\u003e\n\nImplement your app's functionality\n----------------------------------\n\nTo implement your app, refer to\n[Using the Android for Cars App Library](/training/cars/apps) on how Car App\nLibrary apps are built. Also, be sure to familiarize yourself with the\n[Car App Quality Guidelines for IOT apps](/docs/quality-guidelines/car-app-quality?category=iot#app_categories)\n, as your app will be reviewed against these guidelines.\n\nFor IOT apps, the [`GridTemplate`](/reference/androidx/car/app/model/GridTemplate)\nis a great choice for displaying a list of devices and allowing the user to\ninteract with them, as shown in the following sample: \n\n### Kotlin\n\n```kotlin\nval listBuilder = ItemList.Builder()\n\nlistBuilder.addItem(\n GridItem.Builder()\n .setTitle(\"Garage door\")\n .setImage(...)\n // Handle user interactions\n .setOnClickListener {...}\n .build()\n)\n\nlistBuilder.addItem(\n GridItem.Builder()\n .setTitle(\"Garage lights\")\n // Show a loading indicator until the status of the device is known\n // (call invalidate() when the status is known to refresh the screen)\n .setLoading(true)\n .build()\n)\n\nreturn GridTemplate.Builder()\n .setTitle(\"Devices\")\n .setHeaderAction(Action.APP_ICON)\n .setSingleList(listBuilder.build())\n .build()\n```\n\n### Java\n\n```java\nItemList.Builder listBuilder = new ItemList.Builder();\n\nlistBuilder.addItem(\n new GridItem.Builder()\n .setTitle(\"Garage door\")\n .setImage(...)\n // Handle user interactions\n .setOnClickListener(() -\u003e {...})\n .build()\n);\n\nlistBuilder.addItem(\n new GridItem.Builder()\n .setTitle(\"Garage lights\")\n // Show a loading indicator until the status of the device is known\n // (call invalidate() when the status is known to refresh the screen)\n .setLoading(true)\n .build()\n);\n\nreturn new GridTemplate.Builder()\n .setTitle(\"Devices\")\n .setHeaderAction(Action.APP_ICON)\n .setSingleList(listBuilder.build())\n .build();\n```"]]