Overview
在上一篇博客的基础之上,使用Search Dialog来记录搜索的历史。记录用户的搜索记录可以提高用户搜索的效率,例如在用户搜索过test
后,用户输入了t
,将用户搜索过的以t
开头的词test
显示出来。
创建搜索Actiivty
这在上篇博文中,已经创建了,与它类似即可。
创建Content Provider
创建的Content Provider要继承自SearchRecentSuggestionsProvider。
1
2
3
4
5
6
7
8
public class SearchSuggestionProvider extends SearchRecentSuggestionsProvider {
public final static String AUTHORY = "com.example.Test.SearchSuggestionProvider";
public final static int MODE = DATABASE_MODE_QUERIES;
public SearchSuggestionProvider(){
setupSuggestions(AUTHORY, MODE);
}
}
AUTHORITY
是一个唯一的字符串,一般使用该类的完整的名字(包名 + 类名)。DATABASE MODE
一般是DATABASE_MODE_QUERIES
或DATABASE_MODE_QUERIES | DATABASE_MODE_2LINES
。DATABASE_MODE_2LINES
为suggestion表增加了一列,用于给每个建议提供一行文本。
声明Content Provider
在AndroidManifest.xml中,声明上一步创建的Content Provider。
1
<provider android:authorities="com.example.Test.SearchSuggestionProvider" android:name=".SearchSuggestionProvider"></provider>
修改搜索配置
在res/xml/searchable.xml中,增加Content Provider的配置。其中的android:searchSuggestAuthority
必须和Content Provider中的AUTHORITY
保持一致。android:searchSuggestSelection
是一个空格加一个问号。
1
2
3
4
5
6
7
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/app_name"
android:hint="@string/hint"
android:searchSuggestAuthority="com.example.Test.SearchSuggestionProvider"
android:searchSuggestSelection=" ?"
>
</searchable>
保存用户的搜索记录
在上篇博文中的搜索Activity,即MyActivity中,在用户每次执行搜索时,传递来的搜索关键字,保存在Content Provider。saveRecentQuery()
的第二个参数,需和DATABASE_MODE_2LINES
配合使用,否则传入null
。
1
2
3
4
5
6
7
8
Intent intent = getIntent();
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
doSearch(query);
SearchRecentSuggestions srs = new SearchRecentSuggestions(this, SearchSuggestionProvider.AUTHORY, SearchSuggestionProvider.MODE);
srs.saveRecentQuery(query, null);
}
清除用户搜索记录
为了保护用户的隐私,一般需要提供一个清除搜索记录的功能。清除记录仅需调用clearHistory()
方法即可。一般代码如下:
1
2
SearchRecentSuggestions srs = new SearchRecentSuggestions(this, SearchSuggestionProvider.AUTHORY, SearchSuggestionProvider.MODE);
srs.clearHistory();