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. SSM整合dubbo 进行分页查询

    1.先书写Mapper和sql语句 public interface ActEntityMapper { int deleteByPrimaryKey(String actId); int inser ...

  2. IOS mac入门

    https://sarin.iteye.com/blog/1754920 注册Apple开发者平台账号 ## https://www.cnblogs.com/liuluoxing/p/6549725. ...

  3. C++_基础4-分支语句和逻辑运算符

    这一部分截取自<C++ Primer Plus>,内容比较简单,很多只取了一些主题关键词,有空再补充: 设计智能程序的一个关键是使程序具有决策能力. 前面一种方式是循环——程序决定是否继续 ...

  4. HDU - 5050 (大数二进制gcd)

    It's time to fight the local despots and redistribute the land. There is a rectangular piece of land ...

  5. URAL - 1146

    从来不会DP的家伙终于要开始重拾DP了 最大子矩阵没啥好说的,注意单调最大子矩阵不用这么高复杂度,另行更新 #include<bits/stdc++.h> #define rep(i,j, ...

  6. 通过 flume 上传数据到hive

    目标: 通过接受 1084端口的http请求信息, 存储到 hive数据库中, osgi为hive中创建的数据库名称 periodic_report6 为创建的数据表, flume配置如下: a1.s ...

  7. Oracle分组函数之CUBE魅力

    Oracle的CUBE与ROLLUP功能很相似,也是在数据统计分析领域的一把好手. 关于ROLLUP的查询统计功能请参考文章<Oracle分组函数之ROLLUP魅力>(http://www ...

  8. java se系列(三) 顺序语句、if...else、switch、While、do-while、for、break、continue

    1 顺序语句 语句:使用分号分隔的代码称作为一个语句. 注意:没有写任何代码只是一个分号的时候,也是一条语句,称作空语句. 顺序语句就是按照从上往下的顺序执行的语句. 2 判断(if…else) 什么 ...

  9. (转)PEP 8——Python编码风格指南

    PEP 8——Python编码风格指南标签(空格分隔): Python PEP8 编码规范原文:https://lizhe2004.gitbooks.io/code-style-guideline-c ...

  10. Unity中Shader和AssetBundle结合使用的注意事项

    之前遇到了一件事情就是打包安卓的ab后,unity在editor启动下,加载出来的abshader丢失,其实发布安卓后运行是正常的,当时还纠结了半天,还写了个重新赋值的脚本 下面是unity开发的一些 ...