语音控制使驾驶员无需将手离开方向盘或将视线移开路面即可执行任务。借助汽车应用的 App Actions,驾驶员可以通过说出“嘿 Google,在 ExampleApp 上查找路边停车位”之类的指令,使用 Google 助理来控制车载信息娱乐系统上的 Android 应用。
App Actions 适用于 兴趣点 (POI) 汽车应用。本指南涵盖了将 App Actions 集成到您的 POI 应用中的具体要求和限制。
工作原理
App Actions 将您的应用内功能扩展到助理,让用户可以通过语音访问应用功能。当用户调用 App Action 时,助理会将查询与您的应用 shortcuts.xml
资源中声明的内置 Intent (BII) 进行匹配,并在请求的屏幕上启动您的应用。
您可以使用 Android capability
元素在应用中声明对 BII 的支持。当您使用 Google Play 管理中心上传应用时,Google 会注册您应用中声明的功能,并将其提供给用户,以便从助理中访问。
- 用户触发助理并对特定应用发出语音请求。
- 助理会将请求与预训练模型 (BII) 进行匹配,并提取 BII 支持的任何参数。
- 在此示例中,助理将查询与
GET_CHARGING_STATION
BII 进行匹配,提取位置参数“SFO”,并将该位置转换为其地理坐标。 - 该应用通过其针对此 BII 的实现定义被触发。
- 应用处理实现,在驾驶员的车载信息娱乐系统中显示充电站选项。
限制
App Actions 的汽车实现存在以下限制:
汽车应用操作必须使用 Android 深层链接实现。有关 App Actions 实现的信息,请参阅为内置 Intent 提供实现详情。
汽车实现仅支持以下 BII:
- 停车 -
GET_PARKING_FACILITY
- 充电 -
GET_CHARGING_STATION
- 停车 -
要求
执行以下步骤,为 App Actions 准备您的汽车应用:
确定您的 Intent 和实现
使用 App Actions 为汽车应用启用语音功能的第一步是确定您的应用支持哪些用户语音命令或 Intent。然后,您为每个 Intent 定义一个实现,以指定您的应用应如何满足请求。
您的汽车应用支持哪些 Intent?
App Actions 提供预训练的语音模型,称为内置 Intent (BII),当用户说“嘿 Google”时,这些模型可以理解和解释用户的语音命令。要响应语音请求,您只需向助理声明您的应用支持的 BII。例如,如果您希望您的应用协助查找停车设施,您可以实现
GET_PARKING_FACILITY
BII。或者,实现GET_CHARGING_STATION
BII 以帮助用户查找电动汽车充电站。您的应用应如何实现每个 Intent?
您的应用通过启动到适当的屏幕来满足语音请求。App Actions 为您的实现提供从用户请求中提取的参数,使您能够根据用户的需求定制响应。
集成 App Actions
确定您的实现策略后,请按照以下步骤为您的汽车应用启用语音功能:
打开您的主 activity
AndroidManifest.xml
并声明支持 Android 快捷方式。您使用capability
快捷方式元素向助理声明您的应用支持的 BII。有关更多信息,请参阅添加功能。<!-- AndroidManifest.xml --> <meta-data android:name="android.app.shortcuts" android:resource="@xml/shortcuts" />
接下来,将
<intent-filter>
元素添加到AndroidManifest.xml
。这使助理能够使用深层链接连接到您的应用内容。对于 Android Auto 实现,
<intent-filter>
与您的移动应用相同。对于 Android Automotive OS,您的应用的
CarAppService
会话会触发助理。要允许会话触发您的深层链接,请在AndroidManifest.xml
的<activity>
元素中指定一个<intent-filter>
。
<!-- AndroidManifest.xml --> <activity ... android:name="androidx.car.app.activity.CarAppActivity"> ... <intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> <data android:scheme="YOUR_SCHEME" android:host="YOUR_HOST" /> </intent-filter> </activity>
如果您的应用
res/xml
目录中还没有shortcuts.xml
文件,请创建一个新文件。有关 App Actions 如何使用 Android 快捷方式的信息,请参阅创建 shortcuts.xml。在
shortcuts.xml
中,为您选择的 BII 实现一个capability
。接下来,添加一个嵌套的<intent>
来定义应用实现。<!-- shortcuts.xml --> <?xml version="1.0" encoding="utf-8"?> <shortcuts xmlns:android="http://schemas.android.com/apk/res/android"> <capability android:name="actions.intent.GET_PARKING_FACILITY"> <intent> <url-template android:value="YOUR_SCHEME://YOUR_HOST{?name,address,disambiguatingDescription,latitude,longitude}"> <!-- Facility name, e.g. "Googleplex" --> <parameter android:name="parkingFacility.name" android:key="name"/> <!-- Address, e.g. "1600 Amphitheatre Pkwy, Mountain View, CA 94043" --> <parameter android:name="parkingFacility.address" android:key="address"/> <!-- Disambiguate the type of service, e.g. "valet" --> <parameter android:name="parkingFacility.disambiguatingDescription" android:key="disambiguatingDescription"/> <!-- Latitude, e.g. "37.3861" --> <parameter android:name="parkingFacility.geo.latitude" android:key="latitude"/> <!-- Longitude, e.g. "-122.084" --> <parameter android:name="parkingFacility.geo.longitude" android:key="longitude"/> </intent> </capability> </shortcuts>
最后,更新您的汽车应用的
Session()
逻辑以处理传入的 App Actions 实现。以下示例演示了Session.onCreateScreen()
和Session.onNewIntent()
的 Intent 处理。onCreateScreen()
Kotlin
@Override fun onCreateScreen(@NonNull intent: Intent): Screen { if (intent.getData() != null) { val uri: Uri = intent.getData() // uri = "YOUR_SCHEME://YOUR_HOST?name=Levis%20center" // Build your Templates with parsed uri parameters ... } }
Java
@Override public Screen onCreateScreen(@NonNull Intent intent) { if (intent.getData() != null) { Uri uri = intent.getData(); // uri = "YOUR_SCHEME://YOUR_HOST?name=Levis%20center" // Build your Templates with parsed uri parameters ... } }
onNewIntent()
Kotlin
@Override fun onNewIntent(@NonNull intent: Intent): Screen { if (intent.getData() != null) { val uri: Uri = intent.getData() // uri = "YOUR_SCHEME://YOUR_HOST?name=Levis%20center" // Build your Templates with parsed uri parameters ... } }
Java
@Override public void onNewIntent(@NonNull Intent intent) { if (intent.getData() != null) { Uri uri = intent.getData(); // uri = "YOUR_SCHEME://YOUR_HOST?name=Levis%20center" // Build your Templates with parsed uri parameters ... } }
预览、测试并发布您的应用
App Actions 提供用于预览和测试应用的工具。请访问App Actions 概览以获取有关此工具的信息,以及如何将您的语音启用的汽车应用发布到 Play 商店的详细信息。