1. Configure the search description file
Add sreachable. xml to the xml file in res. The content is as follows:
<? Xml version = "1.0" encoding = "UTF-8"?>
<Searchable
Xmlns: android = "http://schemas.android.com/apk/res/android"
Android: hint = "@ string/searchLable"
Android: label = "@ string/searchLable"
Android: searchSuggestAuthority = "com. glacier. ui. SearchSuggestionProvider"
Android: searchSuggestSelection = "? ">
</Searchable>
2. Fill in the configuration file information
1. Search box Configuration
<! -- Search action -->
<Intent-filter>
<Action android: name = "android. intent. action. SEARCH">
</Action>
</Intent-filter>
<Meta-data
Android: name = "android. app. default_searchable"
Android: value = "MainActivity"/>
<Meta-data
Android: name = "android. app. searchable"
Android: resource = "@ xml/searchable">
</Meta-data>
2. Save content Configuration
<Provider
Android: authorities = "com. glacier. ui. SearchSuggestionProvider"
Android: name = "com. glacier. ui. SearchSuggestionProvider">
</Provider>
3. Call the method for starting the search box
// The search box is displayed.
OnSearchRequested ();
You can re-write the system method and load other necessary content.
@ Override
Public boolean onSearchRequested (){
// Open the floating search box (the value of the first parameter added to the search box by default)
StartSearch (null, false, null, false );
Return true;
}
// Obtain the search result
@ Override
Public void onNewIntent (Intent intent ){
Super. onNewIntent (intent );
// Obtain the value in the search box
Query = intent. getStringExtra (SearchManager. QUERY );
System. out. println (query );
// Save search records
SearchRecentSuggestions suggestions = new SearchRecentSuggestions (MainActivity. this,
SearchSuggestionProvider. AUTHORITY, SearchSuggestionProvider. MODE );
Suggestions. saveRecentQuery (query, null );
System. out. println ("saved successfully ");
}
4. Remember where to write Storage
Import android. content. SearchRecentSuggestionsProvider;
Public class SearchSuggestionProvider extends SearchRecentSuggestionsProvider {
Public final static String AUTHORITY = "com. glacier. ui. SearchSuggestionProvider ";
Public final static int MODE = DATABASE_MODE_QUERIES;
Public SearchSuggestionProvider (){
Super ();
SetupSuggestions (AUTHORITY, MODE );
}
}