When writing the Android search code, how to implement the search function, consider two kinds of:
Custom Search methods:
1. Custom Search input box, search icon, search button
2. Custom speech input methods
3. Customize common hot word content, style
4. Customizing browsing history content and styles
5. Custom pop-up style using spinner with ListView
6. Custom data source formats and search algorithms
Custom search is more complicated than it is, and we need to think more.
Android comes with the search method:
1. Create a search dialog box configuration file
2. Create a acitivity that can be used for search and perform a search
3. Accept the search query and invoke the Search dialog box
Omit the pop-up style of the custom algorithm, search the data source format and algorithm two chunks.
Advantages of the Android search method:
1. When you need to provide search services in your application, your first thought is where to put your search box? Process a user's search request by using the Android Search dialog box. With a simple search button or calling an API from your application, the Search dialog box appears at the top of the screen and automatically displays your app icon.
This article implements an application that provides a custom search dialog box, provides a standardized search experience, and adds features such as voice search and search suggestions.
Android's search framework will manage the Search dialog box for you, you don't need to develop a search box yourself, you don't have to worry about where the search box is located, and you don't have to worry about the search box affecting your current interface. All of this work has the Searchmanager class to handle, Searchmanager manages the entire life cycle of the Android Search dialog box, and returns the search keyword after your application sends a search request.
When the user executes a search, Searchmanager will use a dedicated intent to route the keyword of the query to the activity that you configured in the configuration file to process the search results. Essentially, all you need to prepare is an activity to receive intent, then perform a search and give the results, specifically, the things you need to do include:
(1) A search configuration, with an XML configuration file to configure the Search dialog box, including some of the features of the configuration, such as text boxes, set up voice search and search suggestions to display the prompt text. (Searchable.xml)
(2) An activity used to process a search request, which is used to receive the contents of a search query, then search your data and display the search results.
(3) A user-executed search path, by default, once you have configured a searchable activity, the device search key (if present) invokes the Search dialog box. You should then use a button that provides another means for users to be able to invoke search dialogs such as the Search button in the Options menu and other user interface, since not all devices provide a dedicated search key.
First, the search configuration
The search box configuration file is an XML file that is used to configure the application, which is generally named Searchable.xml and must be saved in the project's res/xml/directory. The root node of the configuration file must be searchable and can have one or more properties.
<?xml version= "1.0" encoding= "Utf-8"? ><searchable xmlns:android= "http://schemas.android.com/apk/res/ Android " android:label=" @string/app_name " android:hint=" @string/app_version "></searchable>
in the above configuration file, in addition to the Android:hint property, the other is a search dialog box must be a configuration item, Android:label is a required property, its value is a string resource reference, not directly with the string, Typically, the name of the application (although it is a mandatory attribute, is usually not shown unless you turn on the search suggestions feature). Android:hint is the input prompt for configuring the search box, and you must also reference the configured string resource in String.xml, and you cannot use the string directly. Many properties can be configured, but most properties are only configured with search suggestions and voice searches, although so far as possible, configure Android:hint to prompt the user for information that needs to be entered.
Next, you need to put this configuration file in your application.
Ii. Create an activity that can be used for search
When a user performs a search from a search box, Searchmanager sends the content to be searched (the keyword) through action_search intent to an activity that performs the search. The activity queries the data and displays the results. Create an activity to perform the search, declare that it can respond to Action_search Intent, and add the search box configuration information, for which a node that needs to be added an element and an element in your manifest file. As shown below:
<activity android:name= "com.spring.mainview.SearchActivity" android:theme= "@style/ Default.notitlebar "> <intent-filter> <action android:name=" Android.intent.action.SEARCH "/ > </intent-filter> <meta-data android:name= "android.app.searchable" android:resource= "@ Xml/searchable "/></activity>
The Android:name property value in the code must be "Android.app.searchable", and the Android:resource property value must refer to the search configuration file under the Res/xml/directory mentioned above (res/xml/in this article) Searchable.xml).
Note that only the nodes that are configured with the activity of the Meta-data node above can perform the search, and if you want to call the search box in the entire application, you can configure the following:
<activity android:name= "com.spring.mainview.SearchActivity" android:theme= "@style/ Default.notitlebar "> <intent-filter> <action android:name=" Android.intent.action.SEARCH "/ > </intent-filter> <meta-data android:name= "android.app.searchable" android:resource= "@ Xml/searchable "/></activity><meta-data android:name=" android.app.default_searchable " android: Value= "Com.spring.mainview.SearchActivity"/>
In the code above, andorid:name= "Android.app.default_searchable" defines a name for the search request that responds to the search box, android:value which activity responds and performs the search. When we perform a search request in Otheractivity in the application, Searchactivity will be loaded to perform the search and display the search results.
When an activity is declared searchable, performing an actual search consists of three steps: accepting the query, retrieving your data, and submitting the results. Typically, your search results need to be presented in a ListView, so the activity that is intended to be performed will inherit listactivity, which can be used to access the ListView API.
When you perform a search from the Search dialog box, the activity that you just configured for search will be activated by intent with some search-related parameters, you need to check intent and make a search response as follows:
Intent intent= getintent ();//Determine if the search request is (Intent.ACTION_SEARCH.equals (Intent.getaction ())) {//Gets the search query content (keyword) String query = Intent.getstringextra (searchmanager.query);//execute the corresponding query action domysearch (query);}
The Domysearch () method will query the database based on the keyword or query the data from the network, if it is time-consuming search, you also need to use the search progress bar to tell the user that the search is in progress, and finally return the results, you can call the ListView Setadapter () method to display the results in the ListView.
You can invoke the Onsearchrequested () method from anywhere in the application to activate the search box, such as from a menu or a button. You can also call it in the OnCreate () method. Setdefaultkeymode (default_keys_search_local) so that when the user presses a key on the keyboard, the search box is automatically activated.
The search box, like the normal dialog box, floats at the top of the screen, and he does not change the acitivity stack state, and no method in the activity life cycle is called, and the running activity loses the input focus only when the search box appears.
If you are performing a search, you can override the Onsearchrequested () method, as shown below
@Override Public Boolean onsearchrequested () { //This method is doing what you want to do, such as doing some of the work Pausesomestuff (); return super.onsearchrequested (); }
If the current activity is the activity of the corresponding search request, there are two scenarios:
By default, Action_search intent will create a new activity and call the OnCreate () method, and the new activity will be displayed first, and you will have two activity instances at the same time. When you press the "Back" button, you go back to the activity before the search was performed.
In another case where the activity of android:launchmode= "Singletop" is configured, we need to process the search request in the Onnewintent (Intent) method as follows:
Code
@Overridepublic void OnCreate (Bundle savedinstancestate) { super.oncreate (savedinstancestate); Setcontentview (r.layout.search); Handleintent (Getintent ());} @Overrideprotected void Onnewintent (Intent Intent) { setintent (Intent); Handleintent (intent);} private void Handleintent (Intent Intent) { if (Intent.ACTION_SEARCH.equals (Intent.getaction ())) { String query = Intent.getstringextra (searchmanager.query); Domysearch (query);} }
The corresponding activity configuration is as follows
<activity android:name= ". Mysearchableactivity "android:launchmode=" Singletop "><intent-filter> <action android:name=" Android.intent.action.SEARCH "/></intent-filter><meta-data android:name=" android.app.searchable " Android:resource= "@xml/searchable"/></activity>
To pass parameters to the search box, we need to rewrite the onsearchrequested () method as follows:
@Overridepublic Boolean onsearchrequested () { bundle appdata= new Bundle (); Appdata.putboolean (mysearchableactivity.jargon,true); Startsearch (Null,false, appdata,false); return true;}
Finally, let's take a look at how to use the Android voice search:
Just make the following changes to our search configuration file, your search will support voice search, configuration file
<?xml version= "1.0" encoding= "Utf-8"? ><searchable xmlns:android= "http://schemas.android.com/apk/res/ Android " android:label=" @string/app_name " android:hint=" @string/app_version " android: Voicesearchmode= "Showvoicesearchbutton|launchrecognizer" ></searchable>
Search for content in Android