构建兴趣点应用

本指南详细介绍了车载应用库的不同功能,您可以使用这些功能来实现您的兴趣点 (POI) 应用的功能。

在清单中声明类别支持

您的应用需要在其 CarAppService 的 intent 过滤器中声明 androidx.car.app.category.POI 车载应用类别

以下示例展示了如何声明应用类别

<application>
    ...
   <service
       ...
        android:name=".MyCarAppService"
        android:exported="true">
      <intent-filter>
        <action android:name="androidx.car.app.CarAppService" />
        <category android:name="androidx.car.app.category.POI"/>
      </intent-filter>
    </service>
    ...
<application>

访问地图模板

POI 应用可以访问 PlaceListMapTemplateMapWithContentTemplate

PlaceListMapTemplate 专门用于显示 POI 列表以及由主机渲染的地图。

MapWithContentTemplate 可用于显示列表和其他类型的内容,以及由您的应用渲染的地图。有关使用此模板的更多详情,请参阅绘制地图

要访问这些模板,您的应用需要在其 AndroidManifest.xml 文件中声明 androidx.car.app.MAP_TEMPLATES 权限。

<manifest ...>
  ...
  <uses-permission android:name="androidx.car.app.MAP_TEMPLATES"/>
  ...
</manifest>

刷新 PlaceListMapTemplate 内容

您可以让驾驶员在浏览使用 PlaceListMapTemplate 构建的地点列表时,轻触按钮即可刷新内容。实现 OnContentRefreshListener 接口的 onContentRefreshRequested 方法,并使用 PlaceListMapTemplate.Builder.setOnContentRefreshListener 在模板上设置监听器以启用列表刷新。

以下代码段展示了如何在模板上设置监听器

Kotlin

PlaceListMapTemplate.Builder()
    ...
    .setOnContentRefreshListener {
        // Execute any desired logic
        ...
        // Then call invalidate() so onGetTemplate() is called again
        invalidate()
    }
    .build()

Java

new PlaceListMapTemplate.Builder()
        ...
        .setOnContentRefreshListener(() -> {
            // Execute any desired logic
            ...
            // Then call invalidate() so onGetTemplate() is called again
            invalidate();
        })
        .build();

刷新按钮仅当监听器具有值时才显示在 PlaceListMapTemplate 的标头中。

当用户点击刷新按钮时,您的 OnContentRefreshListener 实现的 onContentRefreshRequested 方法会被调用。在 onContentRefreshRequested 中,调用 Screen.invalidate 方法。然后,主机将回调您的应用的 Screen.onGetTemplate 方法,以检索包含刷新内容的模板。有关刷新模板的更多信息,请参阅刷新模板内容。只要 onGetTemplate 返回的下一个模板类型相同,则它会计为一次刷新,并且不计入模板配额。

使用应用操作与 Google 助理集成

使用助理为您的 POI 应用启用语音功能,允许用户通过询问“Hey Google,在 ExampleApp 上查找附近的充电站”等问题来搜索兴趣点。如需详细说明,请参阅车载应用操作