版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/anhenzhufeng/article/details/87369177
在适配全局搜索功能的时候发现一个类:SearchManager,从来没使用过,这个是用来查询系统内所有支持全局搜索功能的应用。相对应的服务就是SearchManagerService,类似于ActivityManagerService的作用,在系统启动的时候就启动了。
一般用法就是首先获取服务:
mAndroidSearchManager = (SearchManager) mContext.getApplicationContext().getSystemService(Context.SEARCH_SERVICE);
其次就是获取所有支持搜索的应用信息:
List<SearchableInfo> infoList = mAndroidSearchManager.getSearchablesInGlobalSearch(); for (SearchableInfo info : infoList) { String suggestPackage = info.getSuggestPackage();//包名 String suggestAuthority = info.getSuggestAuthority();//其实就是定义contentProvider时候的authority String suggestIntentAction = info.getSuggestIntentAction();//支持的intentAction String suggestIntentData = info.getSuggestIntentData();//额外的数据 String suggestPath = info.getSuggestPath();//能查询的路径 String suggestSelection = info.getSuggestSelection();//检索的条件,也就是Sql语句where后面的条件 ComponentName searchActivity = info.getSearchActivity();//被查询的activity}
然后根据某个SearchableInfo组织成一个uri,最终通过contentResolver去查询。
能被查询的应用本质上就是通过contentProvider来实现跨进程数据共享的。一个应用要想能被SearchManager检索到,则必须要被注册在SearchManagerService中。也就是需要在xml中声明一些属性才能被系统识别继而注册到SMS中。具体需要哪些声明可参考这个:Introducing Quick Search Box for Android
参考文章:SearchManager分析