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

时间:2023-03-08 22:28:17

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

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

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

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

除此之外,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显示用户输入的查询内容:

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

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

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