Search (搜索)是Android平台的一个核心功能之一,用户可以在手机搜索在线的或是本地的信息。Android平台为所有需要提供搜索或是查询功能的应用提 供了一个统一的Search Framework来帮助实现Search功能。Search Framework的UI可以有两种形式:

  • 屏幕顶部的Search Dialog:如Google Map提供的搜索条。
  • 可以嵌到ContentView中的SearchView,应用可以将它放在屏幕上任何地方。

不管采用那种UI,Android系统都可以通过向某个指定Activity发送需要查询的内容来帮助应用实现查询功能。同时Android也支持查询提示,如下图所示:

除此之外,Android查询UI可以支持:

  • 语音查询
  • 根据用户输入提供查询提示列表
  • 支持应用自定义查询提示列表来匹配用户输入
  • 在系统全局搜索(System-wide Quick Search Box)提供你的应用相关的查询提示列表

Invoke Search介绍了如何使用Search Framework 并采用Search dialog 的方式在屏幕顶部显示查询条。下面结合例子介绍使用Search Framework的一般步骤:

Create a Search Interface

例采用屏幕顶部Search Dialog的方式。在这种方式下,Android操作系统接管所有Search Dialog的事件,当用户提交查询后,Android系统将给支持的用来处理查询的Activity发送消息。Search Dialog可以提供查询提示列表来匹配用户输入。

用户提交查询后,Android系统构造一个Intent并把用户的查询内容放在这个Intent中。然后Android启动你定义的用来处理用户查询的 Activity(称为Searchable Activity),并把这个Intent发给该Activity。为了能够使用Android系统提供的Search Framework.需要以下几步:

1. Creating a Searchable Configuration

首先定义一个Searchable configuration,用于描述Search Dialog 的一些属性,该描述文件按惯例通常命名为searchable.xml 并定义在/res/xml 目录下:

<!-- The attributes in this XML file provide configuration information -->
<!-- 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=" ? "
/>

只有android:label是必须的,一般定义为应用程序的名称。尽管不是必须的,一般也会定义android:hint。这个属性定义查询框没有任 何输入时的背景文字。如上图中的”Search the dictionary” 。本例中为“Search Demo Hint”来提示用户可以输入的内容。

2. Creating a Searchable Activity

一个”Searchable Activity”就是一个可以用来处理Search Query 的Activity。和一般的Activity没有太大分别。当用户提交查询后,Android会给这个“Searchable Activity”发送一个Intent包含有用户查询内容,同时这个Intent 含有ACTION_SEARCH action。

由于可以在任何一个Activity中使用Search Dialog或是SearchView,Android需要知道哪个Activity是“Searchable Activity”,这就需要在AndroidManifest.xml中来定义“Searchable Activity”。

本例中 SearchQueryResults 定义为“Searchable Activity”,它在AndroidManifest.xml中定义为:

        <!-- This activity represents the "search" activity in your application, in which -->
<!-- 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 需要在Intent-filter中指定android.intent.action.SEARCH,并在<meta-data>部分指定 searchable configuration (指向res/xml/searchable.xml)

SearchQueryResults 用来处理用户查询请求,本例为简单起见,只是在屏幕上显示用户查询请求的内容。

它用来处理查询请求Intent的代码如下:

        if (Intent.ACTION_SEARCH.equals(queryAction)) {
doSearchQuery(queryIntent, "onNewIntent()");
}
else {
mDeliveredByText.setText("onNewIntent(), but no ACTION_SEARCH intent");
}

如果请求的Action为ACTION_SEARCH,表明该Activity是由Search Dialog触发的,则调用doSearchQuery来显示用户查询内容。此外这个Activity也可以从Launcher启动,此时Action不 含ACTION_SEARCH。

Using the Search Dialog

Search Dialog 先为屏幕上方的浮动窗口,缺省为不可见的。只有当调用onSearchRequested()或是用户按“Search”键时(不是所有设备都有Search钮,在模拟器上可以用F5)Search Dialog才会显示。

为了使用Search Dialog,我们在AndroidManifest.xml定义了Searchable Activity: SearchQueryResults。 如果此时直接运行SearchQueryResults,在模拟器上按F5,将会在屏幕上方显示Search Dialog。

如果现在Invoke Search (SearchInvoke)Activity也可以使用Search Dialog, 也需要在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" /> <!-- This is not the typical way to define android.app.default_searchable, -->
<!-- and we show it here only because we wish to confine the search demo to this -->

<!-- section of the ApiDemos application. --> <!-- For typical applications, it's simpler to define android.app.default_searchable -->
<!-- just once, at the application level, where it serves as a default for all of -->
<!-- the Activities in your package. -->
</activity>

这时按下 onSearchRequest() 或是 “Search”键就显示Search Dialog,按查询键后,将会在SearchQueryResults显示用户输入的查询内容:

此外我们看到这Search Dialog下提供了最近用户输入作为输入提示,如果里面含有敏感信息,可以清除这个列表

    private void clearSearchHistory() {
SearchRecentSuggestions suggestions = new SearchRecentSuggestions(this,
SearchSuggestionSampleProvider.AUTHORITY, SearchSuggestionSampleProvider.MODE);
suggestions.clearHistory();
}

【起航计划 032】2015 起航计划 Android APIDemo的魔鬼步伐 31 App->Search->Invoke Search 搜索功能 Search Dialog SearchView SearchRecentSuggestions的更多相关文章

  1. 【起航计划 002】2015 起航计划 Android APIDemo的魔鬼步伐 01

    本文链接:[起航计划 002]2015 起航计划 Android APIDemo的魔鬼步伐 01 参考链接:http://blog.csdn.net/column/details/mapdigitap ...

  2. 【起航计划 037】2015 起航计划 Android APIDemo的魔鬼步伐 36 App->Service->Remote Service Binding AIDL实现不同进程间调用服务接口 kill 进程

    本例和下个例子Remote Service Controller 涉及到的文件有RemoteService.java ,IRemoteService.aidl, IRemoteServiceCallb ...

  3. 【起航计划 031】2015 起航计划 Android APIDemo的魔鬼步伐 30 App->Preferences->Advanced preferences 自定义preference OnPreferenceChangeListener

    前篇文章Android ApiDemo示例解析(31):App->Preferences->Launching preferences 中用到了Advanced preferences 中 ...

  4. 【起航计划 027】2015 起航计划 Android APIDemo的魔鬼步伐 26 App->Preferences->Preferences from XML 偏好设置界面

    我们在前面的例子Android ApiDemo示例解析(9):App->Activity->Persistent State 介绍了可以使用Shared Preferences来存储一些状 ...

  5. 【起航计划 020】2015 起航计划 Android APIDemo的魔鬼步伐 19 App->Dialog Dialog样式

    这个例子的主Activity定义在AlertDialogSamples.java 主要用来介绍类AlertDialog的用法,AlertDialog提供的功能是多样的: 显示消息给用户,并可提供一到三 ...

  6. 【起航计划 012】2015 起航计划 Android APIDemo的魔鬼步伐 11 App->Activity->Save & Restore State onSaveInstanceState onRestoreInstanceState

    Save & Restore State与之前的例子Android ApiDemo示例解析(9):App->Activity->Persistent State 实现的UI类似,但 ...

  7. 【起航计划 003】2015 起航计划 Android APIDemo的魔鬼步伐 02 SimpleAdapter,ListActivity,PackageManager参考

    01 API Demos ApiDemos 详细介绍了Android平台主要的 API,android 5.0主要包括下图几个大类,涵盖了数百api示例:

  8. 【起航计划 035】2015 起航计划 Android APIDemo的魔鬼步伐 34 App->Service->Local Service Controller

    Local Service Controller 是将LocalService当作“Started”Service来使用,相对于”Bound” Service 来说,这种模式用法要简单得多,Local ...

  9. 【起航计划 034】2015 起航计划 Android APIDemo的魔鬼步伐 33 App->Service->Local Service Binding 绑定服务 ServiceConnection Binder

    本例和下列Local Service Controller 的Activity代码都定义在LocalServiceActivities.Java 中,作为LocalServiceActivities ...

随机推荐

  1. C++ Timer

    Timer机制 这里所说的Timer机制是定时器(Timer),例如在Javascript中就提供定时执行代码的功能.但是在C++标准中暂时没有实现这一功能的函数. Javascript中的Timer ...

  2. SDK,JDk,Unity打包安卓apk

    SDK:软件开发工具包(缩写:SDK.外语全称:Software Development Kit)一般都是一些软件工程师为特定的软件包.软件框架.硬件平台.操作系统等建立应用软件时的开发工具的集合. ...

  3. Luogu P2243 电路维修 双端队列BFS

    当转移的代价是0和一个分明不同的权值时,可以用双端队列BFS去跑(你跑最短路也没问题..QWQ) 而对于这道题,边旋转代价是1,不旋转代价是0:可以直接建图最短路,也可以跑BFS 这个题建图很有意思: ...

  4. 109th LeetCode Weekly Contest Number of Recent Calls

    Write a class RecentCounter to count recent requests. It has only one method: ping(int t), where t r ...

  5. 1152 Google Recruitment (20 分)

    In July 2004, Google posted on a giant billboard along Highway 101 in Silicon Valley (shown in the p ...

  6. flume将数据发送到kafka、hdfs、hive、http、netcat等模式的使用总结

    1.source为http模式,sink为logger模式,将数据在控制台打印出来. conf配置文件如下: # Name the components on this agent a1.source ...

  7. 安装cloudermanager时出现org.spingframework.web.bind.***** host[] is not present at AnnotationMethodHandlerAdapter.java line 738 ****错误(图文详解)(博主推荐)

    不多说,直接上干货! 首先,这个问题,写给需要帮助的朋友们,本人在此,搜索资料近半天,才得以解决.看过国内和国外,资料甚少.特此,写此博客,为了弥补此错误解决的资料少的缘故! 问题详解  解决办法   ...

  8. nyoj 214——单调递增子序列(二)——————【二分搜索加dp】

    单调递增子序列(二) 时间限制:1000 ms  |  内存限制:65535 KB 难度:4   描述 给定一整型数列{a1,a2...,an}(0<n<=100000),找出单调递增最长 ...

  9. BNU 4260 ——Trick or Treat——————【三分求抛物线顶点】

    ial Judge Prev Submit Status Statistics Discuss Next Type: None   None   Graph Theory       2-SAT   ...

  10. 深入理解JavaScript系列(13):This? Yes,this!

    介绍 在这篇文章里,我们将讨论跟执行上下文直接相关的更多细节.讨论的主题就是this关键字.实践证明,这个主题很难,在不同执行上下文中this的确定经常会发生问题. 许多程序员习惯的认为,在程序语言中 ...