以安卓官方Demo为例:
<!-- The default activity of the app; displays search results. --> <activity android:name=".SearchableDictionary" android:launchMode="singleTop"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> <!-- Receives the search request. --> <intent-filter> <action android:name="android.intent.action.SEARCH" /> <!-- No category needed, because the Intent will specify this class component--> </intent-filter> <!-- Points to searchable meta data. --> <meta-data android:name="android.app.searchable" android:resource="@xml/searchable" /> </activity>有两个关键信息: 需要在intent filter配置action “android.intent.action.Search”,相应的在Activity的业务逻辑中应该有对此Action的处理。 配置一个meta-data “android.app.searchable”,向系统注册了一个Searchable对象。这个meta-data对应一个xml文件来描述Searchable对象信息,与Activity绑定。基本上一个Searchable可以看作一个数据源。可以有多个Activity/xml来处理多个数据源。 第2步:创建Searchable Configuration Searchable Configuration即第1步中的xml文件,仍以安卓官方Demo的searchable.xml为例: <searchable xmlns:android="http://schemas.android.com/apk/res/android" android:label="@string/search_label" android:hint="@string/search_hint" android:searchSettingsDescription="@string/settings_description" android:searchSuggestAuthority="com.example.android.searchabledict.DictionaryProvider" android:searchSuggestIntentAction="android.intent.action.VIEW" android:searchSuggestIntentData="content://com.example.android.searchabledict.DictionaryProvider/dictionary" android:searchSuggestSelection=" ?" android:searchSuggestThreshold="1" android:includeInGlobalSearch="true"> </searchable>安卓官方文档和sdk对上述字段有详细的解释,这里举几个重要的说明。 includeInGlobalSearch:表示是否将数据开放给系统搜索。必须置为true。 searchSuggestAuthority:对应于提供数据的Provider的authorities。 searchSuggestIntentData:在数据使用方,即系统搜索,为点击结果条目跳转回App配置Intent Uri时使用。 queryAfterZeroResults:这是一个有利于性能优化的配置。因为需要在用户每次输入一个字母(或汉字)之后都要触发搜索,如果你的数据源是模糊匹配,譬如输入ab时,如果结果为空,意味着ab*的结果都为空,那么可以将此项配置为false。在系统搜索的数据query逻辑中会使用这个字段来减少数据库query次数。建议在适当的时候使用。 这个配置文件的使用可以参考安卓官方demo。 第3步:接入数据 这一步需要做的是配置并实现对系统搜索提供数据的Provider。安卓官方Demo的Provider配置比较简单,以官方文档中的示例参考: <provider android:name="MySuggestionProvider" android:authorities="com.example.MyCustomSuggestionProvider" android:readPermission="com.example.provider.READ_MY_DATA" android:writePermission="com.example.provider.WRITE_MY_DATA"> <path-permission android:pathPrefix="/search_suggest_query" android:readPermission="android.permission.GLOBAL_SEARCH" /> </provider>其中权限 android.permission.GLOBAL_SEARCH是安卓提供的数据保护权限。 详见:https://developer.android.com/reference/android/Manifest.permission.html GLOBAL_SEARCH。 下面看看数据源需要提供哪些字段?安卓官方文档给出了标准字段:https://developer.android.com/guide/topics/search/adding-custom-suggestions.html#SuggestionTable 其中包括两个必选字段和一些可选字段。 一些重要字段: BaseColumns._ID SearchManager.SUGGEST_COLUMN_TEXT_1 SearchManager.SUGGEST_COLUMN_TEXT_2 SearchManager.SUGGEST_COLUMN_ICON_1 SearchManager.SUGGEST_COLUMN_INTENT_ACTION SearchManager.SUGGEST_COLUMN_INTENT_DATA SearchManager.SUGGEST_COLUMN_SHORTCUT_ID 第4步:enable外部数据源 经过上面3步后添加的数据源默认并没有打开,会在系统搜索设置中出现对应的项,需要用户手动打开,系统搜索才会去这个数据源获取数据。设置项的文字描述可以通过Searchable配置。