将 Android 搜索功能集成到您的应用中
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
尝试使用 Compose 方式
Jetpack Compose 是推荐用于 Android 的界面工具包。了解如何在 Compose 中添加搜索功能。
搜索是 Android 上的核心用户功能。用户必须能够搜索他们可以使用的任何数据,无论内容是在设备上还是互联网上。为了帮助为用户打造一致的搜索体验,Android 提供了一个搜索框架来帮助您为自己的应用实现搜索功能。
图 1. 包含自定义搜索建议的搜索对话框。
搜索框架提供了两种搜索输入模式:一种是屏幕顶部的搜索对话框,一种是您可以嵌入 activity 布局的搜索微件 (SearchView
)。无论是哪种情况,Android 系统都会将搜索查询传递给执行搜索的特定 activity,从而协助您实现搜索功能。您还可以让搜索对话框或搜索微件在用户输入内容时提供搜索建议。图 1 显示了包含可选搜索建议的搜索对话框的示例。
设置搜索对话框或搜索微件后,您可以执行以下操作:
- 启用语音搜索。
- 根据最近的用户查询提供搜索建议。
- 提供与您的应用数据中的实际结果匹配的自定义搜索建议。
- 在系统范围的快速搜索框中提供应用的搜索建议。
注意:搜索框架未提供用于搜索您的数据的 API。要执行搜索,您需要使用适合您的数据的 API。例如,如果您的数据存储在 SQLite 数据库中,请使用 android.database.sqlite
API 来执行搜索。
此外,不能保证设备提供专用的搜索按钮,用于在您的应用中调用搜索界面。在使用搜索对话框或自定义界面时,您必须在界面中提供用于激活搜索界面的搜索按钮。如需了解详情,请参阅调用搜索对话框。
以下页面介绍了如何使用 Android 框架来实现搜索功能:
- 创建搜索界面
- 如何设置您的应用以使用搜索对话框或搜索微件。
- 添加近期查询建议
- 如何根据之前使用的查询提供建议。
- 添加自定义建议
- 如何根据您的应用中的自定义数据提供建议,并在系统范围的快速搜索框中提供这些建议。
- 可搜索配置
- 可搜索配置文件的参考文档。其他文档也从特定行为的角度介绍了该配置文件。
保护用户隐私
当您在自己的应用中实现搜索功能时,应采取措施来保护用户的隐私。许多用户认为他们在手机上的活动(包括搜索)是私人信息。为了保护用户隐私,请遵守以下原则:
- 不要将个人信息发送到服务器,但如果必须发送此类信息,不要将其记录下来。
个人信息是指可以识别用户个人身份的任何信息,如用户的姓名、电子邮件地址、结算信息,或可以合理地关联到此类信息的其他数据。如果您的应用在服务器的协助下实现搜索功能,应避免将个人信息与搜索查询一起发送。例如,如果您要搜索某个邮政编码附近的商家,不需要同时发送用户 ID;只需将该邮政编码发送到服务器即可。如果您必须发送个人信息,请避免将其记录下来。如果您必须记录,请非常小心地保护该数据,并尽快将其擦除。
- 为用户提供清除其搜索记录的方法。
搜索框架可帮助您的应用在用户输入内容时提供特定于上下文的建议。有时,这些建议基于先前的搜索或用户在之前的会话中执行的其他操作。用户可能不希望将先前的搜索透露给其他设备用户。如果您的应用提供的建议会透露先前的搜索活动,请实现让用户清除搜索记录的方法。如果您使用 SearchRecentSuggestions
,可以调用 clearHistory()
方法。如果您实现自定义建议,则需要在内容提供程序中提供一种类似的“清除记录”方法,让用户可以执行清除操作。
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-26。
[null,null,["最后更新时间 (UTC):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."]]