"Sail Plan 032" 2015 sail plan Android apidemo Devil Step App->search->invoke Search Search function search Dialog searchview searchrecentsug Gestions

Source: Internet
Author: User

Search is one of the core features of the Android platform, where users can search for online or local information on their phone. The Android platform provides a unified search framework for all applications that need to provide searching or querying capabilities to help implement the search feature. The Search framework's UI can be in two forms:

    • Search Dialog at the top of the screen: such as Google Maps offers.
    • The Searchview can be embedded in the Contentview, and the app can place it anywhere on the screen.

Regardless of the ui,android system, you can help your app implement query functionality by sending content that needs to be queried to a specific activity . At the same time, Android also supports query hints , as shown in:

In addition, the Android query UI can support:

    • Voice query
    • Provide a list of query hints based on user input
    • Support for applying a custom query hint list to match user input
    • Provide a list of query hints related to your app in the system global search (system-wide Quick search Box)

Invoke search describes how to use the search Framework and use Search dialog to display a query bar at the top of the screen. The following example shows the general steps for using the search framework:

Create a Search Interface

Example using the search dialog at the top of the screen. In this way, the Android operating system takes over all search dialog events, and when the user submits the query, the Android system sends a message to the supported activity to process the query. Search dialog can provide a list of query hints to match user input.

After the user submits the query, the Android system constructs a Intent and puts the user's query content in this Intent. Android then launches the activity (called searchable activity) that you define to handle the user's query, and sends the intent to the activity. In order to be able to use the search Framework provided by the Android system. The following steps are required:

1. Creating a searchable Configuration

First define a searchable configuration that describes some of the properties of the search Dialog, which are usually named Searchable.xml and defined in the/res/xml directory as usual:

 This  for the Search Manager. --><searchable xmlns:android= "http://schemas.android.com/apk/res/android"    Android:label= "@string/search_label"    android:hint= "@string/search_hint"        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 name of the application. Although not required, android:hint are generally defined. This property defines the background text when the query box does not have any input. As in the "Search the Dictionary". In this example, "Search Demo Hint" Prompts the user to enter the content.

2. Creating a searchable Activity

A "searchable activity" is an activity that can be used to process search Query. And the general activity is not much different. When the user submits the query, Android will send the "searchable Activity" a intent package containing the user's query content, and this intent contains the Action_search action.

Because you can use search dialog or searchview,android in any activity, you need to know which activity is "searchable activity", This requires the definition of "searchable Activity" in Androidmanifest.xml.

In this example, Searchqueryresults is defined as "searchable Activity", which is defined in Androidmanifest.xml as:

<!--This activity represents the "search" activity in your application, in which-<!--search results is gathered and displayed. --<activity android:name= ". App. Searchqueryresults "Android:label= "@string/search_query_results" > <intent-filter> <action android:name= "Android.inten T.action.main "/> <category android:name=" Android.intent.category.SAMPLE_CODE "/> </ Intent-filter> <!--this intent-filter identifies ThisActivity as "searchable"-<intent-filter><action android:name= "Android.intent.action.SEARCH"/><category android:name= "Android.intent.category.DEFAULT"/> </intent-filter> <!--Thi S metadata entry provides further configuration details forSearches-<!--that is handled by ThisActivity. -<meta-data android:name= "android.app.searchable" android:resource= "@xml/searchable" /c0>/> </activity>

Searchable Activity needs to specify Android.intent.action.SEARCH in the Intent-filter and specify searchable in the <meta-data> section Configuration (point to Res/xml/searchable.xml)

searchqueryresults is used to process user query requests, this example is simply to display the contents of a user query request on the screen.

The code it uses to process the query request intent is as follows:

        if (Intent.ACTION_SEARCH.equals (queryaction)) {            "onnewintent ()");        }         Else {            mdeliveredbytext.settext ("onnewintent (), but no action_search intent");        }

If the requested action is Action_search, indicating that the activity was triggered by SEARCH dialog, call Dosearchquery to display the contents of the user query. This activity can also be started from launcher, where action does not contain action_search.

Using the Search Dialog

Search Dialog is the floating window above the screen, which is not visible by default. Search dialog will only be displayed if you call onsearchrequested () or if the user presses the "search" key (not all devices have a search button and can use F5 on the emulator).

In order to use search Dialog, we defined searchable activity:searchqueryresults in Androidmanifest.xml. If you run searchqueryresults directly at this point, pressing F5 on the emulator will display the search Dialog on top of the screen.

If you are now using the search Dialog with the Invoke search (Searchinvoke) activity, you will also need to do some clarification in Androidmanifest.xml:

<activity android:name= ". App. Searchinvoke "Android:label= "@string/search_invoke" > <intent-filter> <action android:name= "Android.intent.actio N.main "/> <category android:name=" Android.intent.category.SAMPLE_CODE "/> </intent- Filter> <!--This metadata entry causes. App. Searchqueryresults to be thedefaultContext-<!--whenever the user invokes search whileInch ThisActivity. --<meta-data android:name= "Android.app.default_searchable"Android:value= ". App. Searchqueryresults "/><!--This isn't the typical to define android.app.default_searchable, <!--and we show it Here is because we wish to confine the search demo           <!--section of the Apidemos application.<!--for typical applications, it's simpler to define android.app.default_searchable-<!--just Onc E, at the application level, where it serves as adefault  forAll-<!--the activities in your Package. -</activity>

Then press Onsearchrequest () or "search" key to display search Dialog, press the query key, will be searchqueryresults display user input query content:

In addition, we see that the search dialog provides the most recent user input as an input hint, and if it contains sensitive information, you can clear the list .

    Private void clearsearchhistory () {        new searchrecentsuggestions (this,                 Searchsuggestionsampleprovider.authority, Searchsuggestionsampleprovider.mode);        Suggestions.clearhistory ();    }

"Sailing plan 032" 2015 sailing plan Android Apidemo Devil Step App->search->invoke Search Search function search Dialog searchview searchrecent Suggestions

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.