构建物联网应用

物联网应用使用户能够在车内对连接设备执行相关操作。例如,控制某些设备的状态,例如打开车库门、翻转家庭灯光开关或启用家庭安全系统。

在清单中声明类别支持

您的应用需要在它的 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 应用库,了解如何构建汽车应用库应用。此外,务必熟悉 面向物联网应用的汽车应用质量指南,因为您的应用将根据这些指南进行审核。

对于物联网应用,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();