---
title: "Searchable之Recent Query Suggestions（上）"
author: "mmdev"
date: "2011-12-09T10:10:00Z"
tags: []
slug: "searchable-recent-query-suggestions"
draft: false
lastmod: "2011-12-10T16:20:22Z"
id: 1301560
url: "https://mmdev.iteye.com/blog/1301560"
---

原文地址：http://developer.android.com/guide/topics/search/adding-recent-query-suggestions.html

当你使用search dialog or search widget的时候, 你可以提供基于recent search queries的provide search suggestions.

比如所用户以前搜索过"puppies," 当用户开始输入和他一样的时候，这个query将出现在suggestion中.

图一就是一个在search dialog中使用recent query suggestions的界面图。

图一：

![Recent Query Suggestions（上） - hubingforever - 民主与科学](http://developer.android.com/images/search/search-suggest-recent-queries.png)

在开始实现recent search queries之前，你需要在你的程序中实现search dialog or a search widget基本的搜索 。

关于他们的更多内存请参照《[**Searchable**](http://hubingforever.blog.163.com/blog/static/17104057920114101422295/ "阅读全文")》系列

基本&lt;wbr style="line-height:25px"&gt;&lt;div style="line-height:25px"&gt;Recent query suggestions仅仅是保存搜索过的searches。当用户选择了一个suggestions的时候，你的searchable activity将收到一个把suggestion作为搜索关键字的ACTION\_SEARCH intent，其实你的searchable activity曾经也处理过它。&lt;/div&gt; &lt;div style="line-height:25px"&gt; &lt;div style="line-height:25px"&gt;为了提供recent queries suggestions, 你需要以下几步:&lt;/div&gt; &lt;div style="line-height:25px"&gt; &lt;span style="color:#0000ff; line-height:25px"&gt; &lt;/span&gt;&lt;span style="line-height:25px"&gt;A&lt;/span&gt;&lt;span style="color:#0000ff; line-height:25px"&gt;、 实现一个searchable activity&lt;/span&gt; &lt;/div&gt; &lt;div style="line-height:25px"&gt; &lt;span style="color:#0000ff; line-height:25px"&gt; &lt;/span&gt;&lt;span style="line-height:25px"&gt;B&lt;/span&gt;&lt;span style="color:#0000ff; line-height:25px"&gt;、创建一个继承于SearchRecentSuggestionsProvider的content provider类，并在你的manifest中声明它。&lt;/span&gt; &lt;/div&gt; &lt;div style="line-height:25px"&gt; &lt;span style="color:#0000ff; line-height:25px"&gt; &lt;/span&gt;&lt;span style="line-height:25px"&gt;C&lt;/span&gt;&lt;span style="color:#0000ff; line-height:25px"&gt;、在searchable配置文件中配置提供search suggestions的content provider.&lt;/span&gt; &lt;/div&gt; &lt;div style="line-height:25px"&gt; &lt;span style="color:#0000ff; line-height:25px"&gt; &lt;/span&gt;&lt;span style="line-height:25px"&gt;D&lt;/span&gt;&lt;span style="color:#0000ff; line-height:25px"&gt;、当每次执行搜索的时候，把你的搜索关键字保存你的content provider中&lt;/span&gt; &lt;/div&gt; &lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="color:#003366; line-height:25px"&gt;当系统显示search dialog或search widget的时候，就会在它们下面显示 search suggestions。你所需要做的就是为系统提供提取suggestions的source。&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt; &lt;div style="line-height:25px; color:rgb(0,51,102)"&gt;一旦系统识别到了你的searchable activity并且你提供的search suggestions, 当用户键入查询语句的时候，将执行下面的流程:&lt;/div&gt; &lt;div style="line-height:25px"&gt; &lt;span style="line-height:25px"&gt;1&lt;/span&gt;&lt;span style="color:#0000ff; line-height:25px"&gt;.系统取得用户当前已经输入的搜索关键字，并在你的content provider（suggestions）执行查询&lt;/span&gt; &lt;/div&gt; &lt;div style="line-height:25px"&gt; &lt;span style="line-height:25px"&gt;2&lt;/span&gt;&lt;span style="color:#0000ff; line-height:25px"&gt;.你的content provider用Cursor返回一个和搜索关键字相匹配的所有suggestions&lt;/span&gt; &lt;/div&gt; &lt;div style="line-height:25px"&gt; &lt;span style="line-height:25px"&gt;3&lt;/span&gt;&lt;span style="color:#0000ff; line-height:25px"&gt;.系统以列表的形式显示由Cursor提供的suggestions&lt;/span&gt; &lt;/div&gt; &lt;/div&gt; &lt;div style="line-height:25px"&gt; &lt;div style="line-height:25px; color:rgb(0,51,102)"&gt;一旦recent query suggestions被显示, 下面的几种情况之一将发生:&lt;/div&gt; &lt;div style="line-height:25px"&gt; &lt;span style="line-height:25px"&gt;A&lt;/span&gt;&lt;span style="color:#0000ff; line-height:25px"&gt;,如果用户继续键入搜索关键词，或以任何方式改变了搜索关键词，上面的行为将重复，suggestion list也将更新&lt;/span&gt; &lt;/div&gt; &lt;div style="line-height:25px"&gt; &lt;span style="line-height:25px"&gt;B&lt;/span&gt;&lt;span style="color:#0000ff; line-height:25px"&gt;,如果用户执行搜索的话，suggestions将被忽略，用户输入的搜索关键词将通过ACTION\_SEARCH intent传递到你的searchable activity&lt;/span&gt; &lt;/div&gt; &lt;div style="line-height:25px"&gt; &lt;span style="line-height:25px"&gt;C&lt;/span&gt;&lt;span style="color:#0000ff; line-height:25px"&gt;，如果用户选择了一个suggestion, 将使用这个suggested text做为查询关键字从ACTION\_SEARCH intent传递到你searchable activity&lt;/span&gt; &lt;/div&gt; &lt;div style="line-height:25px; color:rgb(0,51,102)"&gt;你的继承自SearchRecentSuggestionsProvider的content provider将自动的做上面的工作，所以实事上你只有很少的代码需要编写。&lt;/div&gt; &lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="font-size:16px; line-height:28px"&gt;&lt;span style="line-height:28px"&gt;创建Content Provider&lt;/span&gt;&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt; &lt;div style="line-height:28px; font-size:16px"&gt; &lt;span style="color:#003366; line-height:28px"&gt;你用于recent query suggestions的content provider必须是一个SearchRecentSuggestionsProvider的实现。实际上它已经为你提供了所有的东西。&lt;/span&gt;&lt;span style="line-height:28px; color:rgb(0,51,102)"&gt;你所需要的就是重写构造函数来加上一行代码&lt;/span&gt; &lt;/div&gt; &lt;div style="line-height:28px; font-size:16px"&gt;&lt;span style="color:#003366; line-height:28px"&gt;比如下面就是一个recent query suggestions的content provider的完整实现。&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="line-height:25px"&gt;&lt;span style="font-size:13px; line-height:23px"&gt;示例1&lt;/span&gt;&lt;span style="font-size:16px; line-height:28px"&gt;：&lt;/span&gt;&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:28px; font-size:16px; font-weight:bold"&gt;&lt;span style="line-height:22px; font-family:monospace; white-space:pre; font-size:14px"&gt;&lt;span style="color:#ff6600; line-height:25px"&gt;&lt;span style="line-height:25px"&gt;public&lt;/span&gt;&lt;span style="line-height:25px"&gt; &lt;/span&gt;&lt;span style="line-height:25px"&gt;class&lt;/span&gt;&lt;span style="line-height:25px"&gt; &lt;/span&gt;&lt;span style="line-height:25px"&gt;MySuggestionProvider&lt;/span&gt;&lt;span style="line-height:25px"&gt; &lt;/span&gt;&lt;span style="line-height:25px"&gt;extends&lt;/span&gt;&lt;span style="line-height:25px"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="line-height:25px"&gt;&lt;span style="color:#ff6600; line-height:25px"&gt;SearchRecentSuggestionsProvide&lt;/span&gt;&lt;span style="color:#3366ff; line-height:25px"&gt;r&lt;/span&gt;&lt;/span&gt;&lt;span style="line-height:25px; color:rgb(51,102,255)"&gt; &lt;/span&gt;&lt;span style="line-height:25px; color:rgb(51,102,255)"&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/div&gt; &lt;pre style="line-height:25px"&gt;&lt;span style="line-height:25px; color:rgb(51,102,255)"&gt; &lt;/span&gt;&lt;span style="line-height:25px; color:rgb(51,102,255)"&gt;public&lt;/span&gt;&lt;span style="line-height:25px; color:rgb(51,102,255)"&gt; &lt;/span&gt;&lt;span style="line-height:25px; color:rgb(51,102,255)"&gt;final&lt;/span&gt;&lt;span style="line-height:25px; color:rgb(51,102,255)"&gt; &lt;/span&gt;&lt;span style="line-height:25px; color:rgb(51,102,255)"&gt;static&lt;/span&gt;&lt;span style="line-height:25px; color:rgb(51,102,255)"&gt; &lt;/span&gt;&lt;span style="line-height:25px; color:rgb(51,102,255)"&gt;String&lt;/span&gt;&lt;span style="line-height:25px; color:rgb(51,102,255)"&gt; AUTHORITY &lt;/span&gt;&lt;span style="line-height:25px; color:rgb(51,102,255)"&gt;=&lt;/span&gt;&lt;span style="line-height:25px; color:rgb(51,102,255)"&gt; &lt;/span&gt;&lt;span style="line-height:25px; color:rgb(51,102,255)"&gt;"com.example.MySuggestionProvider"&lt;/span&gt;&lt;span style="line-height:25px; color:rgb(51,102,255)"&gt;;&lt;/span&gt;&lt;span style="line-height:25px; color:rgb(51,102,255)"&gt;&lt;br style="line-height:25px"&gt;&lt;/span&gt;&lt;span style="line-height:25px; color:rgb(51,102,255)"&gt;public&lt;/span&gt;&lt;span style="line-height:25px; color:rgb(51,102,255)"&gt; &lt;/span&gt;&lt;span style="line-height:25px; color:rgb(51,102,255)"&gt;final&lt;/span&gt;&lt;span style="line-height:25px; color:rgb(51,102,255)"&gt; &lt;/span&gt;&lt;span style="line-height:25px; color:rgb(51,102,255)"&gt;static&lt;/span&gt;&lt;span style="line-height:25px; color:rgb(51,102,255)"&gt; &lt;/span&gt;&lt;span style="line-height:25px; color:rgb(51,102,255)"&gt;int&lt;/span&gt;&lt;span style="line-height:25px; color:rgb(51,102,255)"&gt; MODE &lt;/span&gt;&lt;span style="line-height:25px; color:rgb(51,102,255)"&gt;=&lt;/span&gt;&lt;span style="line-height:25px; color:rgb(51,102,255)"&gt; DATABASE\_MODE\_QUERIES&lt;/span&gt;&lt;span style="line-height:25px; color:rgb(51,102,255)"&gt;;&lt;/span&gt;&lt;span style="line-height:25px; color:rgb(51,102,255)"&gt;&lt;br style="line-height:25px"&gt;&lt;/span&gt;&lt;span style="line-height:25px; color:rgb(51,102,255)"&gt;public&lt;/span&gt;&lt;span style="line-height:25px; color:rgb(51,102,255)"&gt; &lt;/span&gt;&lt;span style="line-height:25px; color:rgb(51,102,255)"&gt;MySuggestionProvider&lt;/span&gt;&lt;span style="line-height:25px; color:rgb(51,102,255)"&gt;()&lt;/span&gt;&lt;span style="line-height:25px; color:rgb(51,102,255)"&gt; &lt;/span&gt;&lt;span style="line-height:25px; color:rgb(51,102,255)"&gt;{&lt;/span&gt;&lt;span style="line-height:25px"&gt;&lt;span style="color:#3366ff; line-height:25px"&gt;&lt;br style="line-height:25px"&gt;&lt;/span&gt;&lt;span style="color:#ff6600; line-height:25px"&gt;setupSuggestions&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#ff6600; line-height:25px"&gt;&lt;span style="line-height:25px"&gt;(&lt;/span&gt;&lt;span style="line-height:25px"&gt;AUTHORITY&lt;/span&gt;&lt;span style="line-height:25px"&gt;,&lt;/span&gt;&lt;span style="line-height:25px"&gt; MODE&lt;/span&gt;&lt;span style="line-height:25px"&gt;);&lt;/span&gt;&lt;/span&gt;&lt;span style="line-height:25px; color:rgb(51,102,255)"&gt;&lt;br style="line-height:25px"&gt;&lt;/span&gt;&lt;span style="line-height:25px; color:rgb(51,102,255)"&gt;}&lt;/span&gt;&lt;span style="line-height:25px; color:rgb(51,102,255)"&gt;&lt;br style="line-height:25px"&gt;&lt;/span&gt;&lt;span style="line-height:25px; color:rgb(51,102,255)"&gt;}&lt;/span&gt;&lt;/pre&gt; &lt;pre style="line-height:25px"&gt;&lt;span style="color:#3366ff; line-height:25px"&gt;调用&lt;/span&gt;&lt;span style="line-height:25px; font-family:Arial,Helvetica,sans-serif; white-space:normal"&gt;&lt;code style="line-height:25px"&gt;&lt;a rel="nofollow" href="http://developer.android.com/reference/android/content/SearchRecentSuggestionsProvider.html#setupSuggestions%28java.lang.String,%20int%29" style="color:rgb(207,121,28); line-height:25px; text-decoration:none"&gt;setupSuggestions()&lt;/a&gt;&lt;/code&gt;需要两个参数，一个是搜索的授权名，另一个是数据库的模式。搜索授权必须是unique的字符串，但是一般最好的就是使用你的content provider的qualified name(包名加上provider的类名; 比如 "com.example.MySuggestionProvider"). 数据库模式必须包含&lt;/span&gt;&lt;span style="line-height:25px; font-family:Arial,Helvetica,sans-serif; white-space:normal"&gt;&lt;code style="line-height:25px"&gt;&lt;a rel="nofollow" href="http://developer.android.com/reference/android/content/SearchRecentSuggestionsProvider.html#DATABASE\_MODE\_QUERIES" style="color:rgb(207,121,28); line-height:25px; text-decoration:none"&gt;DATABASE\_MODE\_QUERIES&lt;/a&gt;，也可以加上可选项&lt;/code&gt;&lt;/span&gt;&lt;span style="line-height:25px; font-family:Arial,Helvetica,sans-serif; white-space:normal"&gt;&lt;code style="line-height:25px"&gt;&lt;a rel="nofollow" href="http://developer.android.com/reference/android/content/SearchRecentSuggestionsProvider.html#DATABASE\_MODE\_2LINES" style="color:rgb(207,121,28); line-height:25px; text-decoration:none"&gt;DATABASE\_MODE\_2LINES&lt;/a&gt;。&lt;/code&gt;它将在suggestions表的基础上再加入一行，它允许你为每个suggestion再添加一行文本。比如，你想让没suggestion都有2行的话，可以这样：&lt;/span&gt;&lt;/pre&gt; &lt;pre style="line-height:25px"&gt;&lt;span style="line-height:25px; font-family:Arial,Helvetica,sans-serif; white-space:normal"&gt;&lt;span style="line-height:25px"&gt;&lt;span style="line-height:25px"&gt;&lt;span style="font-size:13px; line-height:23px"&gt;示例2：&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt; &lt;pre style="line-height:25px"&gt;&lt;span style="font-family:Arial,Helvetica,sans-serif; font-size:16px; color:#ff9900; line-height:28px"&gt;&lt;span style="line-height:25px; white-space:normal"&gt;public final static int MODE = DATABASE\_MODE\_QUERIES | DATABASE\_MODE\_2LINES;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt; &lt;pre style="line-height:25px"&gt;&lt;span style="font-family:Arial,Helvetica,sans-serif; font-size:13px; color:#000080; line-height:23px"&gt;&lt;span style="line-height:25px; white-space:normal"&gt;注意你在manifest为该&lt;/span&gt;&lt;/span&gt;&lt;span style="line-height:25px; font-family:Arial,Helvetica,sans-serif; white-space:normal"&gt;content provide声明的授权必须和&lt;/span&gt;&lt;span style="line-height:22px"&gt;&lt;span style="color:#ff6600; line-height:22px"&gt;setupSuggestions&lt;/span&gt;&lt;/span&gt;&lt;span style="line-height:22px"&gt;&lt;span style="line-height:22px; color:rgb(255,102,0)"&gt;(&lt;/span&gt;&lt;span style="line-height:22px; color:rgb(255,102,0)"&gt;AUTHORITY&lt;/span&gt;&lt;span style="line-height:22px; color:rgb(255,102,0)"&gt;,&lt;/span&gt;&lt;span style="line-height:22px; color:rgb(255,102,0)"&gt; MODE&lt;/span&gt;&lt;span style="line-height:22px"&gt;&lt;span style="color:#ff6600; line-height:25px"&gt;);&lt;/span&gt;&lt;span style="color:#000080; line-height:25px"&gt;中声明的授权一样。&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt; &lt;pre style="line-height:25px"&gt;&lt;span style="line-height:22px"&gt;&lt;span style="line-height:22px"&gt;&lt;span style="color:#000080; line-height:25px"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="line-height:22px"&gt;&lt;span style="line-height:22px"&gt;&lt;span style="color:#000080; line-height:25px"&gt;比如：&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="line-height:25px; font-family:Arial,Helvetica,sans-serif; white-space:normal"&gt;&lt;span style="line-height:25px"&gt;&lt;span style="font-size:13px; line-height:20px"&gt;示例2：&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt; &lt;pre style="line-height:25px"&gt;&lt;span style="line-height:22px"&gt;&lt;span style="line-height:22px"&gt;&lt;/span&gt;&lt;/span&gt;&lt;pre style="line-height:25px"&gt;&lt;span style="line-height:25px; color:rgb(0,0,255)"&gt;&lt;application&gt;&lt;/span&gt;&lt;span style="line-height:25px; color:rgb(0,0,255)"&gt;&lt;br style="line-height:25px"&gt;&lt;/span&gt;&lt;span style="line-height:25px; color:rgb(0,0,255)"&gt;&lt;provider&lt;/span&gt;&lt;span style="line-height:25px; color:rgb(0,0,255)"&gt; &lt;/span&gt;&lt;span style="line-height:25px; color:rgb(0,0,255)"&gt;android:name&lt;/span&gt;&lt;span style="line-height:25px; color:rgb(0,0,255)"&gt;=&lt;/span&gt;&lt;span style="line-height:25px; color:rgb(0,0,255)"&gt;".MySuggestionProvider"&lt;/span&gt;&lt;span style="line-height:25px; color:rgb(0,0,255)"&gt;&lt;br style="line-height:25px"&gt;&lt;/span&gt;&lt;span style="line-height:25px; color:rgb(0,0,255)"&gt;android:authorities&lt;/span&gt;&lt;span style="line-height:25px; color:rgb(0,0,255)"&gt;=&lt;/span&gt;&lt;span style="line-height:25px"&gt;&lt;span style="color:#0000ff; line-height:25px"&gt;"&lt;/span&gt;&lt;span style="color:#ff9900; line-height:25px"&gt;com.example.MySuggestionProvider&lt;/span&gt;&lt;span style="color:#0000ff; line-height:25px"&gt;"&lt;/span&gt;&lt;/span&gt;&lt;span style="line-height:25px; color:rgb(0,0,255)"&gt; &lt;/span&gt;&lt;span style="line-height:25px; color:rgb(0,0,255)"&gt;/&gt;&lt;/span&gt;&lt;span style="line-height:25px; color:rgb(0,0,255)"&gt;&lt;br style="line-height:25px"&gt; ...&lt;br style="line-height:25px"&gt;&lt;/span&gt;&lt;span style="line-height:25px; color:rgb(0,0,255)"&gt;&lt;/application&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/pre&gt; &lt;/div&gt; &lt;/wbr&gt;