在Android快速搜索中搜索历史记录

时间:2010-11-18 07:24:42

标签: android history quick-search

我正在自定义快速搜索以显示来自我的应用的数据。它的工作正常。现在的问题是,当我点击搜索按钮时,我无法看到搜索历史记录。我该怎么做才能获得搜索记录(以前搜索过的关键字)?

1 个答案:

答案 0 :(得分:2)

如果您浏览developer.android.com上的教程,我想您会找到您想要的内容:

http://developer.android.com/guide/topics/search/adding-recent-query-suggestions.html

诀窍是实现一个扩展SearchRecentSuggestionsProvider的ContentProvider。这是一个简单的课程:

public class MySuggestionProvider extends SearchRecentSuggestionsProvider {
  public final static String AUTHORITY = "com.example.MySuggestionProvider";
  public final static int MODE = DATABASE_MODE_QUERIES;

  public MySuggestionProvider() {
      setupSuggestions(AUTHORITY, MODE);
  }
}

请记住将您的提供程序添加到清单中,并更新您的searchable.xml文件,以便它知道您的提供程序:

<searchable xmlns:android="http://schemas.android.com/apk/res/android"
  android:label="@string/app_label"
  android:hint="@string/search_hint"
  android:searchSuggestAuthority="com.example.MySuggestionProvider"
  android:searchSuggestSelection=" ?" >
</searchable>

此外,您需要将搜索保存在可搜索的活动中:

if (Intent.ACTION_SEARCH.equals(Intent .getAction())) {
  String query = Intent .getStringExtra(SearchManager.QUERY);
  SearchRecentSuggestions suggestions = new SearchRecentSuggestions(this,
      MySuggestionProvider.AUTHORITY, MySuggestionProvider.MODE);
  suggestions.saveRecentQuery(query, null);
}