Read It

Read It 是 Google 助理在 Android 设备上提供的一项功能,让用户可以通过另一种方式阅读长篇网络内容,例如新闻报道和博文。用户可以这样说 “Hey Google, read it”,让应用大声朗读网络内容,突出显示正在朗读的字词,并自动滚动页面。要详细了解这项功能,您也可以阅读 Read It 产品更新博文

When prompted, an app reads web content on the screen out loud with
            the help of the Google Assistant.
图 1. 正在听应用大声朗读网络内容。

包含网络内容的 Android 应用可以通过使用 onProvideAssistContent() 方法向助理提供信息来支持 Read It。

此流程有助于在与助理分享数据时保持数据结构。接收共享应用内容的用户可以直接通过深层链接或直接接收内容,而不是以文本或屏幕截图的形式。

为您应用中的任何网络内容以及任何可共享的 entity 实现 onProvideAssistContent()

向助理提供内容

要让 Read It 访问您的内容,您的应用必须向助理提供有关该内容的信息,例如其网络 URI 和一些基本上下文。然后,助理可以检索您的内容,以便大声朗读给用户听。

对于已经使用 WebViews 或 Chrome Custom Tabs 实现网络内容的 Android 应用,可以使用相同的网络 URI 作为 Read It 的起点。

将 Read It 功能与内置意图结合使用时,您只需在用户在调用 App Action 后的任务流中的最终应用 Activity 中实现 onProvideAssistContent() 即可。

例如,如果您的应用显示新闻报道,请在显示报道的最终屏幕中实现 onProvideAssistContent();您无需为任何正在进行或预览屏幕实现它。

AssistContenturi 字段中为您的内容提供一个网络 URI。在 structuredData 字段中,使用 schema.org 词汇表JSON-LD 对象的形式提供上下文信息。

以下代码段展示了向助理提供内容的示例

Kotlin

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

    // Set the web URI for content to be read from a
    // WebView, Chrome Custom Tab, or other source
    val urlString = url.toString()
    outContent.setWebUri(Uri.parse(urlString))

    // Create JSON-LD object based on schema.org structured data
    val structuredData = JSONObject()
        .put("@type", "Article")
        .put("name", "ExampleName of blog post")
        .put("url", outContent.getWebUri())
        .toString()
    outContent.setStructuredData(structuredData)
}

Java

@Override
public void onProvideAssistContent(AssistContent outContent) {

  // Set the web URI for content to be read from a
  // WebView, Chrome Custom Tab, or other source
  String urlString = url.toString();
  outContent.setWebUri(Uri.parse(urlString));

  try {
      // Create JSON-LD object based on schema.org structured data
      String structuredData = new JSONObject()
          .put("@type", "Article")
          .put("name", "ExampleName of blog post")
          .put("url", outContent.getWebUri())
          .toString();
      outContent.setStructuredData(structuredData);
  } catch (JSONException ex) {
      // Handle exception
      Log.e(TAG, ex.getMessage());
  }

  super.onProvideAssistContent(outContent);
}

实现 onProvideAssistContent() 时,请尽可能提供有关每个 entity 的数据。以下字段是必需的:

  • @type
  • .name
  • .url(仅当内容可通过 URL 访问时需要)

要详细了解如何使用 onProvideAssistContent(),请参阅 Android 开发者文档中的优化助理的上下文内容指南。