Google 助理分享

Android 手机用户可以使用“Ok Google,把这个内容发送给小珍”这样的语音指令让 Google 助理向其他用户分享应用内容。然后,Google 助理可以根据第一位用户的系统选项,将屏幕文本或设备屏幕截图加入到分享内容中。

Google 助理根据提示将选择的照片插入到了消息中。
图 1. Google 助理将照片分享给联系人。

通常情况下,这种分享方式就够用了,但是从您的应用收到分享内容的用户可能不会重新进入应用来查看内容。您可以通过实现 onProvideAssistContent() 方法向 Google 助理提供关于当前前台内容的结构化信息。

此过程有助于在向其他用户分享数据时保持数据的结构。然后,收到分享的应用内容的用户就可以打开相应的深层链接或直接接收内容,而不是接收文本或屏幕截图。

为应用中的所有可共享 entity 实现 onProvideAssistContent()

向 Google 助理提供内容

您只需在调用与应用有关的 Action 后,为用户任务流中的最终应用 activity 实现 onProvideAssistContent() 即可。例如,在 CREATE_MONEY_TRANSFER 任务流中,您可以在显示收据的最终屏幕中实现该方法;您无需为任何进行中的或预览的屏幕实现此方法。

AssistContentstructuredData 字段中使用 schema.org 词汇表,以 JSON-LD 对象的形式提供上下文信息。以下代码段展示了记录上下文内容的示例:

Kotlin
override fun onProvideAssistContent(outContent: AssistContent) {
    super.onProvideAssistContent(outContent)

    // JSON-LD object based on Schema.org structured data
    outContent.structuredData = JSONObject()
            .put("@type", "MenuItem")
            .put("name", "Blueberry Crisp Iced Signature Latte")
            .put("url", "https://mysite.com/menuitems/12345a")
            .toString()
}
      
Java
@Override
public void onProvideAssistContent(AssistContent outContent) {
  super.onProvideAssistContent(outContent);

  // JSON-LD object based on Schema.org structured data
  outContent.structuredData = new JSONObject()
          .put("@type", "MenuItem")
          .put("name", "Blueberry Crisp Iced Signature Latte")
          .put("url", "https://mysite.com/menuitems/12345a")
          .toString();
}
      

尽可能多地提供有关每个 entity 的数据。以下字段是必填字段:

  • @type
  • .name
  • .url(仅当内容可通过网址寻址时才需填写)

如需详细了解如何使用 onProvideAssistContent(),请参阅优化 Google 助理的上下文内容指南。