Read It 是 Google 助理提供的 Android 裝置功能,可讓使用者以多種方式閱讀長篇網頁內容,例如新聞報導和網誌文章。使用者可以說出「Ok Google,朗讀內容」之類的指令,讓應用程式朗讀網頁內容、醒目顯示正在閱讀的字詞,以及自動捲動網頁。如要進一步瞭解這個功能,您也可以參閱「Read It 產品更新文章」。
有網頁式內容的 Android 應用程式,可以使用 onProvideAssistContent()
方法向 Google 助理提供資訊以支援 Read It 功能。
此程序會與 Google 助理共用資料,協助維持資料結構。如此一來,當使用者收到分享的應用程式內容時,即可使用深層連結或直接收到內容,而不是用文字或螢幕截圖。
建議您為應用程式中任何網頁式內容及任何可分享的 entity
實作 onProvideAssistContent()
。
提供內容給 Google 助理
如要讓 Read It 存取內容,應用程式必須提供內容相關資訊,例如網路 URI 和一些基本背景資訊。然後 Google 助理可擷取您的內容,以便系統大聲朗讀。
如果是已經使用 WebView 或 Chrome 自訂分頁實作網路內容的 Android 應用程式,請以相同的網路 URI 做為起始點。
將 Read It 功能與內建意圖結合使用時,只需在叫用應用程式動作後,針對使用者的工作流程實作 onProvideAssistContent()
,即可完成最終的應用程式活動。
舉例來說,如果應用程式顯示新聞報導,請在顯示報導內容的最終畫面中實作 onProvideAssistContent()
,不必針對執行中或預覽畫面實作。
在 AssistContent
的 uri
欄位中,為您的內容提供網路 URI。在 structuredData
欄位中使用 schema.org 詞彙,以 JSON-LD 物件的形式提供內容資訊。
以下程式碼片段為向 Google 助理提供內容的範例:
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
(只有在內容是可藉由網址存取時才是必填資訊)
如果想進一步瞭解如何使用 onProvideAssistContent()
,請看 Android 開發人員文件的「為 Google 助理最佳化背景內容」指南。