Android 8.0 为设置菜单添加了扩展的搜索功能。本文档介绍了如何添加设置并确保它已正确编入设置搜索的索引。
创建可编入索引的设置
每个需要编入索引的设置 Fragment 都会实现 Indexable 接口,并且需要静态字段
public static final SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER
在为 Fragment 设置索引后,将其添加到 SearchIndexableResources,该文件位于
packages/apps/Settings/src/com/android/settings/search/SearchIndexableResources.java
可选方法
此 SearchIndexProvider 接口有四个可选方法。
getXmlResourcesToIndex
- 如果您的 Fragment 内容来自:
preference xml,请替换此方法 - 返回要编入索引的 XML 偏好设置列表。
XML 资源示例
public List<SearchIndexableResource> getXmlResourcesToIndex(Context context, boolean enabled) { ArrayList<SearchIndexableResource> result = new ArrayList<SearchIndexableResource>(); SearchIndexableResource sir = new SearchIndexableResource(context); sir.xmlResId = R.xml.display_settings; result.add(sir); return result; }
getRawDataToIndex
- 如果您的 Fragment 内容并非来自:
preference xml,请替换此方法 - 返回要编入索引的原始数据列表 (
SearchIndexableRaw)。
原始数据示例
public List<SearchIndexableRaw> getRawDataToIndex(Context context, boolean enabled) {
final List<SearchIndexableRaw> result = new ArrayList<>();
final Resources res = context.getResources();
// Add fragment title
SearchIndexableRaw data = new SearchIndexableRaw(context);
data.title = res.getString(R.string.wifi_settings);
data.screenTitle = res.getString(R.string.wifi_settings);
data.keywords = res.getString(R.string.keywords_wifi);
data.key = DATA_KEY_REFERENCE;
result.add(data);
return result;
}getNonIndexableKeys
- 如果您的 Fragment 是
DashboardFragment,您很少需要替换此方法。 - 返回与不应针对给定用户、设备、配置等显示的结果对应的键列表。此处提供的键应与
SearchIndexableResource和SearchIndexableRaw中的 KEY 字段匹配。 - 例如:对于设备中从未插入过 SIM 卡的用户,不应显示数据用量。
不可编入索引的键示例
public List<String> getNonIndexableKeys(Context context) {
final List<String> keys = super.getNonIndexableKeys(context);
if (!checkIntentAction(context, "android.settings.TERMS")) {
keys.add(KEY_TERMS);
}
if (!checkIntentAction(context, "android.settings.LICENSE")) {
keys.add(KEY_LICENSE);
}
if (!checkIntentAction(context, "android.settings.COPYRIGHT")) {
keys.add(KEY_COPYRIGHT);
}
if (!checkIntentAction(context, "android.settings.WEBVIEW_LICENSE")) {
keys.add(KEY_WEBVIEW_LICENSE);
}
return keys;
}getPreferenceControllers
返回与此 Fragment 关联的偏好设置控制器列表。此列表用于形成内嵌结果、更新不可编入索引项等。
因此,您想要在搜索中显示的所有内容都必须包含在 getXmlResourcesToIndex 或 getRawDataToIndex 中。
为您的设置添加关键字
为确保可以轻松搜索到某个设置,请添加与用户可能用于搜索该设置的设置相关的关键字。
添加关键字时要考虑的事项
- 关键字是用户不一定看到但可能是他们设置工作方式心智模型一部分的字词列表。
- 这些是用户可能为访问您的设置而输入的字词。
- 它们可以是同义词或与设置关联的任何字词。
- 例如,“静音”可能用于查找音量设置。
避免重复
如果您无条件地禁止显示某个设置页面,请移除原始页面的索引,以避免结果重复。
- 找到您要禁止显示的页面的
PreferenceFragment。 - 移除
SearchIndexProvider。
验证
要测试新设置的可搜索性
- 在设备上安装 O 的最新版本。
- 通过选择以下选项,重新索引数据库 设置 > 应用和通知 > 应用信息 > 设置 > 存储空间 > 清除数据
- 验证目标设置是否显示在搜索结果中。
搜索设置标题的前缀将匹配该设置。
可以运行这些 Robolectric 测试来验证此功能的实现情况
packages/apps/Settings/tests/robotests/src/com/android/settings/search
构建目标是:RunSettingsRoboTests