2.接下来定义两个String,这里用到了SearchProvider.TYPE_CALL。通过定义呼叫类型,采用localSearch函数将其读出并修改格式,显示在TextView上。SearchProvider为OPhone平台上的一个服务(“服务”前文介绍),该class提供了一个程序内部的搜索框架,文档说:Local search API consists of two parts, one is ContentProvider, which can parse given query string and return Cursor for hitting results, application using search ContentProvider need to handle the search result itself; another is Intent, which launch local search Activity with given arguments.该服务的TYPE_CALL是一个query string,即搜索对象的类型,这是呼叫的类型,还有如下类型: 3.创建一个TextView,这里我们注意到,创建时用到了资源的引用。
接下来,我们简要了解一下localsearch函数的功能。该函数并不是LocalSearch,区分大小写的哦。考虑到上传图片附件宽度限制,我将代码直接贴上来。 public String localSearch(String searchSelection) {
Uri uri = Uri.parse(SearchProvider.CONTENT_URI); Cursor cursor = getContentResolver().query(uri, null, searchSelection, null, null); StringBuffer result = new StringBuffer(); result.append("#id #calltype #title #time(#duration)\n"); // print result out while (cursor.moveToNext()) { // Use cursor.respond function to get the data. Bundle extras = new Bundle(); extras = cursor.respond(extras); // Extract the data from search result String id = extras.getString(SearchProvider.FIELD_ID); String calltype = extras.getString(SearchProvider.FIELD_CALL_TYPE); String title = extras.getString(SearchProvider.FIELD_TITLE); long time = Long.parseLong(extras.getString(SearchProvider.FIELD_TIME)); int duration = Integer.parseInt(extras.getString(SearchProvider.FIELD_CALL_DURATION)); result.append("\n").append(id) .append("\n[").append(calltype).append("]") .append("\t").append(title) .append("\t").append(new Date(time).toString()) .append("(").append(duration).append(")") .append("\n"); } cursor.close(); return result.toString(); }