Android Search Framework examples of detailed _android

Source: Internet
Author: User

Basic knowledge

The Android search framework will be the search dialog for you to manage, you don't need to develop a search box yourself, don't worry about where to put the search box, and don't worry about the search box affecting your current interface. All of this work is handled by the Searchmanager class for you (hereinafter referred to as "search Manager"), it manages the entire lifecycle of the Android Search dialog box, and executes the search request that your application will send, returning the corresponding search keyword.

When a user performs a search, the search Manager uses a dedicated intent to pass the keyword of the search query to the activity that you configured in the configuration file to process the search results. Essentially, all you need is an activity to receive intent, then perform the search and give the results. Specifically, the things you need to do include the following:

A search configuration

We use an XML configuration file to configure the Search dialog box, including some feature configuration, such as text boxes, set voice search and search suggestions for the suggested text to display.

An activity to process a search request

This activity is used to receive the content of the search query, and then search your data and display the search results.

A way for a user to perform a search

By default, once you have configured a searchable activity, the device search key (if any) will invoke the Search dialog box. However, you should always provide another means to allow the user to invoke the Search dialog box, such as the Search button in the Options menu or other user interface buttons, because not all devices provide a dedicated search key.

Create a search dialog box configuration file

The search box profile is an XML file that configures the settings for the search box in your 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, and can have one or more properties. As shown below:

<?xml version= "1.0" encoding= "Utf-8"?> <searchable xmlns:android= 
"Http://schemas.android.com/apk/res" /android " 
android:label=" @string/searchlabel "android:hint=" @string/searchhint "> 

In the above configuration file, in addition to the Android:hint attribute, the other is a required configuration item for a search dialog box, Android:label is a required property, its value is a string resource reference, cannot be used directly with a string, It is usually the name of the application (although it is a required property, it is usually not displayed unless you turn on the search suggestion feature). Android:hint is the input hint for configuring the search box, and you must refer to the string resources configured in String.xml, and you cannot use strings directly.

Many properties can be configured, but most of the properties are only configured when using search advice and voice search, although we recommend that you configure Android:hint to prompt the user for information that needs to be entered.

Next, you need to put this configuration file into your application.

Create an activity that can be used for searching

When a user performs a search from a search box, the search manager sends the content to search (the keyword) via Action_search Intent to an activity that performs the search. This acitivity queries the data and displays the results.

Define a searchable activity

If you are not ready, create an activity to perform the search, stating that it responds to Action_search Intent and increases the search box configuration information. To do this, you need to add an element and an element to the node in your manifest file. As shown below:

<application > 
<activity android:name= ". Mysearchableactivity "> 
<intent-filter> 
<action android:name=" Android.intent.action.SEARCH " > 
</intent-filter> 
<meta-data android:name= "android.app.searchable" 
android:resource= "@ Xml/searchable "/> 
</activity> ... 
</application>

The Android:name property value in must be "android.app.searchable", and Android:resource attribute value must refer to the search configuration file in the res/xml/directory mentioned above (in this case, the res/xml/ Searchable.xml).

Note that only nodes that have an activity configured with the above Meta-data node can perform the search, and if you want to invoke the search box throughout your application, you can configure the following:

<application > 
<activity android:name= ". Mysearchableactivity "> 
<intent-filter> 
<action android:name=" Android.intent.action.SEARCH " > 
</intent-filter> 
<meta-data android:name= "android.app.searchable" 
android:resource= "@ Xml/searchable "/> 
</activity> 
<activity android:name=". Anotheractivity "... > 
</activity> 
<!-This configuration allows you to invoke the search box throughout the application--> 
<meta-data Android:name= "android.app.default_searchable" 
android:value= ". Mysearchableactivity "/> ... 
 

In the code above, android:name= "Android.app.default_searchable" defines the name of a response search box search request, Android:value specifies which activity responds to and performs the search. When we execute the search request in the otheracitivity in the application, Mysearchableactivity will be loaded to perform the search and display the search results.
Perform a search

When an activity is declared searchable, performing the actual search involves three steps: receiving the query, retrieving your data, and submitting the results.

Normally, your search results need to be displayed in a ListView, so the acitivity you use to perform the search will inherit listactivity, which makes it easy to access the ListView API.

Receive search queries

When you perform a search from the Search dialog box, the acitivity that you just configured to use for the search will be activated by intent, and with some search-related parameters, you need to check intent and make a search response as follows:

@Override public 
void OnCreate (Bundle savedinstancestate) { 
super.oncreate (savedinstancestate); 
Setcontentview (r.layout.search); 
Intent Intent = Getintent (); 
Determine if Search request 
if (Intent.ACTION_SEARCH.equals (Intent.getaction ()) { 
//Get search query Content (keyword) 
String query = Intent.getstringextra (searchmanager.query); 
Execute the corresponding query action 
domysearch (query); 
} 

The Domysearch () method will query the database according to the keyword, or query the data from the network, if it is time-consuming search, you also need to use the progress bar, to tell the user that the search is underway, and finally return the results, you can call the ListView Setadapter () method to display the results in ListView.

Invoke the Search dialog box

You can invoke the Onsearchrequested () method from anywhere in your application to activate the search box, such as from a menu or a button. You also want to invoke Setdefaultkeymode (default_keys_search_local) in the OnCreate () method so that the search box is automatically activated when the user presses a key on the keyboard.

The search box, like a normal dialog box, floats at the top of the screen, it does not change any activity stack state, and no methods in the activity lifecycle are invoked, but when the search box appears, the running activity loses the input focus.

If you want to do something else when you perform a search, you can rewrite the onsearchrequested () method as follows:

@Override Public 
Boolean onsearchrequested () { 
//This method of doing what you want to do, such as doing some of the 
pausesomestuff work (); 
return super.onsearchrequested (); 

If the current activity is an activity that responds to a search request, there are two scenarios:

By default, Action_search intent will create a new activity and invoke the OnCreate () method, the new activity will appear at the front, and you will have two instances of activity at the same time. When you press the "Back" button, you return to an activity before the search is performed.

Another scenario is to configure the android:launchmode= "Singletop" activity, at which point we need to process the search request in the Onnewintent (Intent) method, as follows:

@Override public 
void OnCreate (Bundle savedinstancestate) { 
super.oncreate (savedinstancestate); 
Setcontentview (r.layout.search); 
Handleintent (Getintent ()); 
} 
@Override 
protected 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 "/> 

How to add parameters to the search box

To pass parameters to the search box, we need to rewrite the onsearchrequested () method as follows:

@Override Public 
Boolean onsearchrequested () { 
Bundle AppData = new Bundle (); 
Appdata.putboolean (Mysearchableactivity.jargon, true); 
Startsearch (null, FALSE, AppData, false); 
return true; 

Our activity. When you receive a search request for a search box, you get the parameters by using the following method:

Bundle AppData = Getintent (). Getbundleextra (searchmanager.app_data); 
if (AppData!= null) { 
Boolean jargon = Appdata.getboolean (Mysearchableactivity.jargon); 
}

Finally, let's look at how to use the Android voice search:

Just make the following changes to our search profile, your search will support voice search, and the configuration file looks like this:

<?xml version= "1.0" encoding= "Utf-8"?> <searchable xmlns:android= 
"Http://schemas.android.com/apk/res" /android " 
android:label=" @string/searchlabel " 
android:hint=" @string/searchhint " 
android: Voicesearchmode= "Showvoicesearchbutton|launchrecognizer" > 

The above is a small set up to introduce the Android search framework examples of detailed, I hope to help you, if you have any questions welcome to my message, small series will promptly reply to everyone!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.