將 Android 搜尋功能整合至您的應用程式
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
試試 Compose
Jetpack Compose 是 Android 推薦的 UI 工具包。瞭解如何在 Compose 中新增搜尋功能。
搜尋是 Android 的核心使用者功能。使用者必須能夠搜尋可存取的任何資料,無論內容位於裝置上或網際網路上。為協助使用者獲得一致的搜尋體驗,Android 提供搜尋架構,可協助您為應用程式實作搜尋功能。
圖 1. 提供自訂搜尋建議的搜尋對話方塊。
搜尋架構提供兩種搜尋輸入模式:畫面頂端的搜尋對話方塊,或可嵌入活動版面配置的搜尋小工具 (SearchView
)。無論是哪種情況,Android 系統都會將搜尋查詢傳送至執行搜尋的特定活動,協助您實作搜尋功能。您也可以啟用搜尋對話方塊或小工具,在使用者輸入內容時提供搜尋建議。圖 1 顯示搜尋對話方塊範例,其中包含選用的搜尋建議。
設定搜尋對話方塊或搜尋小工具後,您可以執行下列操作:
- 啟用語音搜尋。
- 根據使用者最近的查詢提供搜尋建議。
- 提供與應用程式資料中實際結果相符的自訂搜尋建議。
- 在全系統的快速搜尋框中提供應用程式的搜尋建議。
注意:搜尋架構不會提供 API 來搜尋資料。如要執行搜尋,您必須使用適合資料的 API。舉例來說,如果資料儲存在 SQLite 資料庫中,請使用 android.database.sqlite
API 執行搜尋。
此外,裝置不一定會提供專用的「搜尋」按鈕,用於叫用應用程式中的搜尋介面。使用搜尋對話方塊或自訂介面時,您必須在 UI 中提供搜尋按鈕,以啟動搜尋介面。詳情請參閱「叫用搜尋對話方塊」。
下列頁面說明如何使用 Android 架構實作搜尋功能:
- 建立搜尋介面
- 如何設定應用程式,以便使用搜尋對話方塊或搜尋小工具。
- 新增近期查詢建議
- 如何根據先前使用的查詢提供建議。
- 新增自訂建議
- 如何根據應用程式的自訂資料提供建議,並在全系統的快速搜尋框中顯示建議。
- 可供搜尋的設定
- 可搜尋設定檔的參考文件。其他文件也會根據特定行為討論設定檔。
保護使用者隱私
在應用程式中導入搜尋功能時,請採取相關步驟保護使用者隱私權。許多使用者認為手機上的活動 (包括搜尋) 屬於私人資訊。為保護使用者隱私,請遵守下列原則:
- 請勿將個人資訊傳送至伺服器,如果必須傳送,請勿記錄。
個人資訊是指任何可識別使用者身分的資訊,例如姓名、電子郵件地址、帳單資訊,或是可合理連結至這類資訊的其他資料。如果應用程式是透過伺服器輔助實作搜尋功能,請避免連同搜尋查詢一併傳送個人資訊。舉例來說,如果您要搜尋特定郵遞區號附近的商家,就不需要同時傳送使用者 ID,只要將郵遞區號傳送至伺服器即可。如果必須傳送個人資訊,請避免記錄這類資訊。如果必須記錄,請務必謹慎保護這類資料,並盡快清除。
- 提供清除搜尋記錄的方法。
搜尋架構可協助應用程式在使用者輸入內容時,提供特定情境的建議。有時,這些建議會根據使用者在先前工作階段中的搜尋記錄或其他動作提供。使用者可能不希望其他裝置使用者看到先前的搜尋記錄。如果應用程式提供的建議可能會揭露先前的搜尋活動,請實作讓使用者清除搜尋記錄的方法。如果您使用 SearchRecentSuggestions
,可以呼叫 clearHistory()
方法。如果您要實作自訂建議,必須在內容供應器中提供類似的「清除記錄」方法,供使用者執行。
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-08-26 (世界標準時間)。
[null,null,["上次更新時間:2025-08-26 (世界標準時間)。"],[],[],null,["Try the Compose way \nJetpack Compose is the recommended UI toolkit for Android. Learn how to add search functionality in Compose. \n[Search bar →](/develop/ui/compose/components/search-bar) \n\nSearch is a core user feature on Android. Users must be able\nto search any data that is available to them, whether the content is located on the device or\nthe internet. To help create a consistent search experience for users, Android provides a\nsearch framework that helps you implement search for your application. \n\n**Figure 1.** A search dialog with custom\nsearch suggestions.\n\nThe search framework offers two modes of search input: a search dialog at the top of the\nscreen or a search widget ([SearchView](/reference/android/widget/SearchView)) that you can embed in your activity\nlayout. In either case, the Android system assists your search implementation by\ndelivering search queries to a specific activity that performs searches. You can also enable\nthe search dialog or widget to provide search suggestions as the user types. Figure 1 shows an\nexample of the search dialog with optional search suggestions.\n\nOnce you set up either the search dialog or the search widget, you can do the following:\n\n- Enable voice search.\n- Provide search suggestions based on recent user queries.\n- Provide custom search suggestions that match actual results in your application data.\n- Offer your application's search suggestions in the system-wide Quick Search Box.\n\n**Note** : The search framework does *not* provide APIs to\nsearch your data. To perform a search, you need to use APIs appropriate for your data. For example,\nif your data is stored in an SQLite database, use the [android.database.sqlite](/reference/android/database/sqlite/package-summary)\nAPIs to perform searches.\n\n\u003cbr /\u003e\n\n\nAlso, there is no guarantee that a device provides a dedicated SEARCH button that invokes the\nsearch interface in your application. When using the search dialog or a custom interface, you\nmust provide a search button in your UI that activates the search interface. For more\ninformation, see [Invoke the search\ndialog](/develop/ui/views/search/search-dialog#InvokingTheSearchDialog).\n\nThe following pages show you how to use Android's framework to implement search:\n\n**[Create a search interface](/develop/ui/views/search/search-dialog)**\n: How to set up your application to use the search dialog or search widget.\n\n**[Add recent query\nsuggestions](/develop/ui/views/search/adding-recent-query-suggestions)**\n: How to provide suggestions based on queries previously used.\n\n**[Add custom suggestions](/develop/ui/views/search/adding-custom-suggestions)**\n: How to provide suggestions based on custom data from your application and offer them\n in the system-wide Quick Search Box.\n\n**[Searchable configuration](/develop/ui/views/search/searchable-config)**\n: A reference document for the searchable configuration file. The other\n documents also discuss the configuration file in terms of specific behaviors.\n\nProtect user privacy\n\nWhen you implement search in your application, take steps to protect the user's\nprivacy. Many users consider their activities on their phone---including searches---to\nbe private information. To protect users' privacy, abide by the following\nprinciples:\n\n- **Don't send personal information to servers‐and if you must, don't log it.**\n\n Personal information is any information that can personally identify your users, such as their\n names, email addresses, billing information, or other data that can be reasonably linked to such\n information. If your application implements search with the assistance of a server, avoid sending\n personal information along with the search queries. For example, if you are searching for businesses\n near a ZIP code,\n you don't need to send the user ID as well; send only the ZIP code to the server. If you must\n send personal information, avoid logging it. If you must log it, protect that data\n very carefully and erase it as soon as possible.\n- **Provide users with a way to clear their search history.**\n\n The search framework helps your application provide context-specific suggestions while the user\n types. Sometimes these\n suggestions are based on previous searches or other actions taken by the user in an earlier\n session. A user might not want previous searches to be revealed to other device users. If your\n application provides suggestions that\n can reveal previous search activities, implement a way for the user to clear their\n search history. If you are using [SearchRecentSuggestions](/reference/android/provider/SearchRecentSuggestions),\n you can call the\n [clearHistory()](/reference/android/provider/SearchRecentSuggestions#clearHistory())\n method. If you are implementing custom suggestions, you need to provide a similar \"clear history\"\n method in your content provider that the user can execute."]]