构建物联网应用

借助 IoT 应用,用户可以在车内对已连接的设备执行相关操作。例如控制某些设备的状态,包括打开车库门、开关家里的电灯或启用住宅安全设备。

在清单 (manifest) 中声明类别支持

应用需要在其 CarAppService 的 intent 过滤器中声明 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 for Cars 应用库,了解如何构建汽车应用库类别的应用。此外,请务必熟悉针对 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();