用途:
根据指定的URI路径信息返回包含特定数据的Cursor对象,应用这个方法可以使Activity接管返回数据对象的生命周期。
参数:
URI: Content Provider 需要返回的资源索引
Projection: 用于标识有哪些columns需要包含在返回数据中。
Selection: 作为查询符合条件的过滤参数,类似于SQL语句中Where之后的条件判断。
SelectionArgs: 同上。
SortOrder: 用于对返回信息进行排序。
下面是有关ContentResolver.query()和Activity.managedQuery()两个方法的区别:
To query a content provider, you can use either the ContentResolver.query() method or the Activity.managedQuery() method. Both methods take the same set of arguments, and both return a Cursor object. However, managedQuery() causes the activity to manage the life cycle of the Cursor. A managed Cursor handles all of the niceties, such as unloading itself when the activity pauses, and requerying itself when the activity restarts. You can ask an Activity to begin managing an unmanaged Cursor object for you by calling Activity.startManagingCursor() .
例子:
Cursor c = managedQuery(allCalls, null, null, null, null);String[] projection = new String[] { Calls._ID, Calls.NUMBER, Calls.TYPE}; Cursor c = managedQuery(allCalls, projection, null, null, null); Cursor c = managedQuery(allCalls, projection, "Calls.NUMBER LIKE '65%'", //---retrieve numbers beginning with 65 null, null); Cursor c = managedQuery(allCalls, projection, "Calls.NUMBER LIKE '5%'", null, "Calls.TYPE DESC"); //---sort result by call TYPE descending
|
|