Search history in Android quick search
I am customizing quick search to display data from my app. Its working fine. Now the i开发者_运维百科ssue is, When I click on search button, I am not able to see the search history. What should I do get the search history (previously searched keywords)?
If you go through the tutorial on developer.android.com, I think you will find what you are looking for:
http://developer.android.com/guide/topics/search/adding-recent-query-suggestions.html
The trick is to implement a ContentProvider which extends SearchRecentSuggestionsProvider. It's a simple class:
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);
}
}
Remember to add your provider to the manifest, and update your searchable.xml file so that it knows of your provider:
<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>
Also you will need to save the searches in your searchable activity:
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);
}
精彩评论