Compiler une application IoT
Restez organisé à l'aide des collections
Enregistrez et classez les contenus selon vos préférences.
Les applications IoT permettent aux utilisateurs d'effectuer des actions pertinentes sur des appareils connectés depuis leur voiture. Vous pouvez, par exemple, contrôler l'état de certains appareils, comme ouvrir une porte de garage, actionner des interrupteurs ou activer la sécurité de la maison.
Déclarer la catégorie compatible dans votre fichier manifeste
Votre application doit déclarer la catégorie d'applications automobiles androidx.car.app.category.IOT
dans le filtre d'intent de son CarAppService
.
<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>
Implémenter les fonctionnalités de votre application
Pour implémenter votre application, consultez la page Utiliser la bibliothèque d'applications Android for Cars sur la création d'applications de la bibliothèque d'applications pour voitures. Veillez également à vous familiariser avec les Consignes relatives à la qualité des applications de voiture pour les applications IoT, car votre application doit les respecter.
Dans le cas des applications IoT, GridTemplate
constitue un excellent choix pour afficher une liste d'appareils et autoriser l'utilisateur à interagir avec ceux-ci, comme illustré dans l'exemple suivant :
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();
Le contenu et les exemples de code de cette page sont soumis aux licences décrites dans la Licence de contenu. Java et OpenJDK sont des marques ou des marques déposées d'Oracle et/ou de ses sociétés affiliées.
Dernière mise à jour le 2025/07/27 (UTC).
[null,null,["Dernière mise à jour le 2025/07/27 (UTC)."],[],[],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```"]]