Search is one of the core functions of the Android platform. You can Search online or local information on your mobile phone. The Android platform provides a unified Search Framework for all applications that require Search or query functions to help implement the Search function. The Search Framework UI can be in two forms:
Search Dialog at the top of the screen: Search entries provided by Google Map.
It can be embedded in the SearchView in ContentView, and the application can place it anywhere on the screen.
Regardless of the UI, the Android system can send the content to be queried to a specified Activity to help the application implement the query function. Android also supports query prompts, as shown in:
In addition, the Android query UI supports:
Voice Query
Provides a list of query prompts based on user input.
Supports the application custom query prompt list to match user input.
Provides a list of application-related query prompts in the System-wide Quick Search Box.
Invoke Search describes how to use the Search Framework and display query entries at the top of the screen using the Search dialog method. The following describes how to use the Search Framework in combination with examples:
Create a Search Interface
This example uses the Search Dialog method at the top of the screen. In this way, the Android operating system takes over all Search Dialog events. After a user submits a query, the Android system sends a message to the supported Activity to process the query. Search Dialog provides a list of query prompts to match user input.
After a user submits a query, the Android system constructs an Intent and places the user's query content in this Intent. Android then starts the Activity you defined to process user queries (called Searchable Activity) and sends the Intent to the Activity. To use the Search Framework provided by the Android system, take the following steps:
1. Creating a Searchable Configuration
First, define a Searchable configuration to describe some attributes of the Search Dialog. This description file is typically named searchable. xml and defined under the/res/xml directory by convention.
<Searchable xmlns: android = "http://schemas.android.com/apk/res/android"
Android: label = "@ string/search_label"
Android: hint = "@ string/search_hint"
Android: searchMode = "showSearchLabelAsBadge"
Android: voiceSearchMode = "showVoiceSearchButton | launchRecognizer"
Android: voiceLanguageModel = "free_form"
Android: voicePromptText = "@ string/search_invoke"
Android: searchSuggestAuthority = "com. example. android. apis. SuggestionProvider"
Android: searchSuggestSelection = "? "
/>
Only android: label is required and is generally defined as the application name. Although not required, android: hint is generally defined. This attribute defines the background text when the query box does not contain any input. For example, "Search the dictionary ". In this example, "Search Demo Hint" is used to prompt users to enter the content.
2. Creating a Searchable Activity
A "Searchable Activity" is an Activity that can be used to process Search queries. There is not much difference with a general Activity. After a user submits a query, Android sends an Intent containing the user's query content to the Searchable Activity, and the Intent contains the ACTION_SEARCH action.
Because you can use Search Dialog or SearchView in any Activity, Android needs to know which Activity is "Searchable Activity". Therefore, you need to define "Searchable Activity" in AndroidManifest. xml ".
In this example, SearchQueryResults is defined as "Searchable Activity", which is defined in AndroidManifest. xml:
<! -Search results are gathered and displayed.->
<Activity android: name = ". app. SearchQueryResults"
Android: label = "@ string/search_query_results">
<Intent-filter>
<Action android: name = "android. intent. action. MAIN"/>
<Category android: name = "android. intent. category. SAMPLE_CODE"/>
</Intent-filter>
<! -This intent-filter identifies this activity as "searchable"->
<Intent-filter>
<Action android: name = "android. intent. action. SEARCH"/>
<Category android: name = "android. intent. category. DEFAULT"/>
</Intent-filter>
<! -This metadata entry provides further configuration details for searches->
<! -That are handled by this activity.->
<Meta-data android: name = "android. app. searchable"
Android: resource = "@ xml/searchable"/>
</Activity>
Searchable Activity needs to specify android. Intent. action. SEARCH in intent-filter, and specify searchable configuration (pointing to res/xml/searchable. xml) in <meta-data>)
SearchQueryResults is used to process user query requests. In this example, the content of the user query request is displayed on the screen.
The code used to process the Intent query request is as follows:
[Java]
If (Intent. ACTION_SEARCH.equals (queryAction )){
DoSearchQuery (queryIntent, "onNewIntent ()");
}
Else {
MDeliveredByText. setText ("onNewIntent (), but no ACTION_SEARCH intent ");
}
If (Intent. ACTION_SEARCH.equals (queryAction )){
DoSearchQuery (queryIntent, "onNewIntent ()");
}
Else {
MDeliveredByText. setText ("onNewIntent (), but no ACTION_SEARCH intent ");
}
If the requested Action is ACTION_SEARCH, it indicates that the Activity is triggered by the Search Dialog, doSearchQuery is called to display the user's query content. In addition, this Activity can be started from Launcher. Action does not include ACTION_SEARCH.
Using the Search Dialog
Search Dialog is a floating window at the top of the screen, which is invisible by default. Only when onSearchRequested () is called or you press the "Search" Key (not all devices have the Search button, you can use F5 on the simulator) Will the Search Dialog be displayed.
To use the Search Dialog, we define Searchable Activity: SearchQueryResults in AndroidManifest. xml. If SearchQueryResults is run directly at this time, press F5 on the simulator to display the Search Dialog on the top of the screen.
If the current Invoke Search (SearchInvoke) Activity can also use the Search Dialog, you also need to make some instructions in AndroidManifest. xml:
<Activity android: name = ". app. SearchInvoke"
Android: label = "@ string/search_invoke">
<Intent-filter>
<Action android: name = "android. intent. action. MAIN"/>
<Category android: name = "android. intent. category. SAMPLE_CODE"/>
</Intent-filter>
<! -This metadata entry causes. app. SearchQueryResults to be the default context->
<! -Whenever the user invokes search while in this Activity.->
<Meta-data android: name = "android. app. default_searchable"
Android: value = ". app. SearchQueryResults"/>
</Activity>.
Press onSearchRequest () or "Search" to display the Search Dialog. After you press the query key, the query content entered by the user is displayed in SearchQueryResults:
In addition, we can see that the latest user input is provided in the Search Dialog as the input prompt. If it contains sensitive information, we can clear this list.
[Java]
Private void clearSearchHistory (){
SearchRecentSuggestions suggestions
= New SearchRecentSuggestions (this,
SearchSuggestionSampleProvider. AUTHORITY,
SearchSuggestionSampleProvider. MODE );
Suggestions. clearHistory ();
}
Author: mapdigit