Add search for Android apps

Source: Internet
Author: User

BKJIA Translation: Two tutorials in this series will guide developers in developing search functions using the Android search framework. In the first article, this article will allow developers to understand the Android search framework. In the second tutorial, we will integrate the search recommendations and global search into the app.

Local Search VS global search

Let's take a look at the differences between local search and global search in the Android search framework.

Local Search:

It refers to the search function provided by the app itself, which is best for any app, for example, a recipe app should allow users to search by keyword in this app. Local Search is performed in an app. Different apps cannot search for each other.

Global Search

On the other hand, global search allows users to expand related searches in various apps Based on keywords through the quick search box on the home screen. Android uses multiple data sources to help global search. For example, in the Android tablet system, you can see that the left part is the user-input search content, using google search and the retrieved results, it can even contain the titles of apps installed on users' machines. They are displayed on the right.

The user experience for global search is completely different from that for local search. In the global search function, you can use google to search for apps, address book, and other apps installed on the local machine, or even the search results of some apps that allow global search. The data source that can be searched globally is displayed, including web, app, music, message, and address book.

As you can see, a good app should appear as soon as possible, so that users can search for it first, making it easier for users to perform operations. In Article 1 of this tutorial, we will provide more detailed instructions on how to perform global search.

Enable search in the APP

In an app, you must perform at least three steps as follows to enable the app to search. If you want to provide search recommendations, you also need to perform Step 1:

Configure the XML configuration file for search

First, let's take a look at how to configure the search XML configuration file. Name the configuration file name searchable. xml and save it in the res/xml folder. Then you need to set the text in the search box and add a hint text, as shown in the following code:

 
  1. <searchable  
  2. xmlns:android="http://schemas.android.com/apk/res/android" 
  3. android:label="@string/search_label"> 
  4. android:hint="@string/search_hint" 
  5. </searchable> 

There are many configuration options for searching the configuration file. We recommend that you refer to the Android manual for more information:

Http://developer.android.com/guide/topics/search/searchable-config.html.

Add Search Activity

Android calls activity to perform a search. The Code is as follows:

 
  1. Publicclass SampleSearchActivity extends ListActivity {
  2. Public void onCreate (Bundle savedInstanceState ){
  3. Super. onCreate (savedInstanceState );
  4. HandleIntent (getIntent ());
  5. }
  6. Public void onCreate (Bundle savedInstanceState ){
  7. Super. onCreate (savedInstanceState );
  8. HandleIntent (getIntent ());
  9. }
  10. Public void onNewIntent (Intent intent ){
  11. SetIntent (intent );
  12. HandleIntent (intent );
  13. }
  14. Public void onListItemClick (ListView l,
  15. View v, int position, long id ){
  16. // Processing code for each search result
  17. }
  18. Private void handleIntent (Intent intent ){
  19. If (Intent. ACTION_SEARCH.equals (intent. getAction ())){
  20. String query =
  21. Intent. getStringExtra (SearchManager. QUERY );
  22. DoSearch (query );
  23. }
  24. }
  25. Private void doSearch (String queryStr ){
  26. // Execute real query result Processing
  27. }
  28. }

In the above Code, when you press the search button in the handleIntent method, the system will automatically send Intent, and the action is Intent. ACTION_SEARCH, and then use intent. getStringExtra (SearchManager. QUERY); obtain the string to be searched.

Why does the onNewIntent () method need to be included? This is mainly because Android's back-to-back mechanism. Android will place each new activity on the top of the activity stack by default. If you click the back key, the activity at the top of the stack is closed. Try to consider a situation where the user searches for a content and the system lists the results. If the user finds that the results are not what he wants, or wants to re-search, the user clicks the search button again, this will generate a new search activity instance. There will be two search activities in the activity stack, which developers do not expect. Therefore, the activity to be searched must be declared as a singleTop activity. In this way, no matter whether the user presses the return key or tries to search multiple times, in the acitivty stack, a search activity instance is always maintained. Because when the activity is set to the loading mode of singleTop, if the Activity already exists at the top of the stack, it will not be re-created, but will call onNewIntent. If the Activity exists but is not on the top, the Activity will be re-created.

Mainifest configuration file

Next, you must configure the manifest configuration file as follows:

  • Activity to be searched.
  • Use the search intent
  • Activity Startup Mode
  • Searchable. xml Metadata
  • More definitions of search metadata

The following is a typical search configuration.

 
  1. <application 
  2. android:icon="@drawable/icon" 
  3. android:label="@string/app_name" 
  4. android:name=".YourApp" > 
  5. <meta-data 
  6. android:name="android.app.default_searchable" 
  7. android:value=".YourSearchActivity" /> 
  8. <activity 
  9. android:label="@string/app_name" 
  10. android:launchMode="singleTop" 
  11. android:name=".YourSearchActivity" > 
  12. <intent-filter > 
  13. <action android:name="android.intent.action.SEARCH" /> 
  14. </intent-filter> 
  15. <intent-filter > 
  16. <action android:name="android.intent.action.VIEW" /> 
  17. </intent-filter> 
  18. <meta-data 
  19. android:name="android.app.searchable" 
  20. android:resource="@xml/searchable" /> 
  21. </activity> 
  22. </application> 

In the preceding typical configuration, pay attention to the following points:

1) Android calls android when searching for activity. intent. action. as the SEARCH intent, SEARCH must include android in intent-filter. intent. action. SEARCH.

2) in <meta-data>, the position of searchable. xml is pointed out.

3) in <meta-data>:

 
  1. <meta-data 
  2. android:name="android.app.default_searchable" 
  3. android:value=".YourSearchActivity" /> 

Specifies the activity that will be called for processing when the string to be searched is submitted.

Custom Search

For app applications, it is best to customize the search function and clearly tell users that your app supports search.

Note that there is no search button in the Honeycomb and Ice Cream Sndewich versions of Android. The search button is displayed in the action bar. Therefore, a good way is to add a search button to the actionbar at the same time, as if it was running on Android 2.2.

When you start searching, you need to open a Search dialog box in the activity. This can be done by calling onSearchRequested (). This is the method of the Activity class in Android and startsearch () is called () method, which will be discussed later.

Add specified data to search

The default search sometimes does not meet your needs. You may need to use global search or some additional data. In this case, you can rewrite the onSearchRequestd () method and call the startSearch method by yourself, the parameters defined in the startSearch method are as follows:

Parameters

Type

Description

InitialQuery

String

Default string in the search box

SelectInitialQuery

Boolean

This flag indicates whether the content in the search box can be selected. If it is set to true, it overwrites initialQuery.

AppSearchData

Bundle

Other data passed to the Search activity

GlobalSearch

Boolean

Determine whether to use global search

When the startSearch () method is called by default, the following values are used by default: null, false, null, and false. The meaning is that only local search is used and no other values are appended.

Enable apps to support Voice Search

If the APP allows users to search by voice, it is more convenient for users! In Android, you only need to add some code. When the Voice settings are used, a microphone will appear in Android to allow users to use the voice.

However, note that not all devices support voice search. To use voice search, you must set the following configuration options:

 
  1. android:voiceSearchMode= 
  2. "showVoiceSearchButton|launchRecognizer" 

Among them, voiceSearchMode has two options: showVoiceSearchButton and launchRecognizer. Among them, showVoiceSearchButton is required. We recommend that you use the launchRecognizer option. Another important option is android: voiceLanguageModel, which has two options: web_search and free_form. It is recommended that you use web_search, which means to search on the Web using the language model, this is suitable for scenarios where users are not very accurate in reading phrase sentences, while the free_form option is suitable for example in some applications such as SMS or email. Note that voice retrieval must be used in a networked environment.

Summary

This topic describes how to use the basic framework of Android search and how to add automatic prompt for search in the next tutorial, and how to make your APP appear in the Android search menu. We look forward to the next translation of BKJIA.

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.