如何将自己的App作为外部数据源提供给Android系统搜索?

    xiaoxiao2021-03-25  88

    (1)基础:安卓标准search框架 search框架是安卓为了减少App开发搜索功能的成本,提供的一套接口,主要实现两大类功能:第一、可以轻松地实现本地数据的搜索,包括数据和UI;第二、提供了将App的数据开放给系统搜索(system-wide Quick Search Box)的能力。第二部分基于第一部分,在实现了第一部分的基础上,应用可以通过简单的配置即将数据开放出来。 安卓将第二部分命名为“Custom Suggestion”,即在搜索输入query string的时候,即时推荐的内容。这部分相当于给了App一个数据通道,可以通过它将自己的数据在系统搜索框中被搜索到,增加入口。 安卓官方文档对于Search框架有详细的介绍: https://developer.android.com/guide/topics/search/index.html 安卓官方demo: https://github.com/android/platform_development/tree/master/samples/SearchableDictionary/ 安卓代码中也提供了这个demo的源代码: development/samples/SearchableDictionary 建议先通读官方guide。本文接下来基于官方文档以及demo,整理接入的steps,只要按照文档逐步实现即可。本文仅包含数据开放给系统搜索的部分,App内部搜索不涉及。 (2)如何将自己的App作为一个外部数据源添加到系统搜索 第1步:创建Search Activity 这个Activity用以处理搜索结果的跳转,在系统搜索中点击搜索召回的结果,跳转到的界面即Search Activity。

    以安卓官方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配置。

    转载请注明原文地址: https://ju.6miu.com/read-20700.html

    最新回复(0)