Search framework is a set of interfaces provided by Android to reduce the cost of developing search function for App. It mainly implements two kinds of functions: first, it can easily search local data, including data and UI; second, it provides the ability to open App's data to system-wide Quick Search Box. The second part is based on the first part. On the basis of realizing the first part, the application can open up the data through simple configuration.
Android named the second part "Custom Suggestion", which refers to the content recommended instantly when searching for query string s. This part is equivalent to giving App a data channel, through which its own data can be searched in the system search box, adding entries.
The Search framework is described in detail in Android's official documentation:
https://developer.android.com/guide/topics/search/index.html
Android official demo:
https://github.com/android/platform_development/tree/master/samples/SearchableDictionary/
The source code for this demo is also provided in Android code:
development/samples/SearchableDictionary
It is suggested to read through the official guide first. Next, based on official documents and demo, the access steps are sorted out, as long as they are implemented step by step according to documents. This article only contains the part of data open to system search, which is not involved in App internal search.
(2) How to add your App as an external data source to the system search
Step 1: Create Search Activity
This activity is used to handle the jump of search results, click on the result of search recall in system search, and jump to the interface of Search Activity.
Take Android's official Demo for example:
There are two key messages:<!-- 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>
It is necessary to configure Action "android.intent.action.Search" in intent filter, which should be handled in Activity's business logic accordingly.
Configure a meta-data "android.app.searchable" to register a Searchable object with the system. This meta-data corresponds to an xml file that describes Searchable object information and binds to Activity. Basically a Searchable can be seen as a data source. There can be multiple Activities / xml to process multiple data sources.
Step 2: Create Searchable Configuration
Searchable Configuration, the xml file in Step 1, still takes searchable.xml of Android's official Demo as an example:
Android official documents and sdk have detailed explanations of the above fields. Here are some important explanations.<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>
IncludeIn Global Search: Indicates whether data is open to system search. Must be set to true.
SearchSuggest Authority: The authorities that correspond to the Provider that provides the data.
SearchSuggest IntentData: Used when data users, i.e. system searches, jump back to App to configure Intent Uri for clicking results.
queryAfterZeroResults: This is a configuration that is conducive to performance optimization. Because a search needs to be triggered every time a user enters a letter (or Chinese character), if your data source is a fuzzy match, such as when you enter ab, if the result is empty, which means that the result of ab* is empty, then you can configure this to false. This field is used in the data query logic of the system search to reduce the number of database queries. It is recommended that it be used at an appropriate time.
This profile can be used with reference to Android's official demo.
Step 3: Access data
What we need to do in this step is to configure and implement a Provider that provides data for system search. Android's official Demo Provider configuration is relatively simple. Refer to the example in the official document:
The permission android.permission.GLOBAL_SEARCH is Android's data protection permission. If your data source does not want to be used by App other than system search, you can declare that you need this permission.<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>
See: https://developer.android.com/reference/android/Manifest.permission.html GLOBAL_SEARCH.
Let's look at what fields the data source needs to provide. The official Android document gives the standard fields: https://developer.android.com/guide/topics/search/add-custom-suggestions.html#SuggestionTable
It includes two required fields and some optional fields.
Some important fields:
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 SearchManager.SUGGEST_NEVER_MAKE_SHORTCUT
Step 4: enable external data source
After the above three steps, the added data source does not open by default, and the corresponding items will appear in the system search settings. The user will need to open it manually, and the system search will go to the data source to obtain data. The text description of the settings can be configured through Searchable.