managedQuery() will use ContentResolver's query(). The difference is that with managedQuery() the activity will keep a reference to your Cursor and close it whenever needed (in onDestroy() for instance.) If you do query() yourself, you will have to manage the Cursor as a sensitive resource. If you forget, for instance, to close() it in onDestroy(), you will leak underlying resources (logcat will warn you about it.)
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().
分享到:
相关推荐
查询数据时,使用ContentResolver.query()方法,传入适当的参数。例如,可以指定需要返回的列(projection),选择特定的行(selection),设置筛选参数(selectionArgs)以及排序顺序(sortOrder)。返回的Cursor...
然而,从Android 4.4开始,`managedQuery()`已被废弃,取而代之的是`getContentResolver().query()`。同时,为了兼容新旧API,我们可以根据设备的Android版本选择使用`ACTION_GET_CONTENT`或`ACTION_OPEN_DOCUMENT`...
查询操作通常通过 `ContentResolver.query()` 或 `Activity.managedQuery()` 方法执行。后者除了返回 `Cursor` 对象外,还会自动管理 `Cursor` 的生命周期,例如在 Activity 暂停或销毁时关闭 `Cursor`,以防止资源...
在Android中,`managedQuery`和`query`都是用于查询数据库的方法,但它们存在一定的区别。 **1. managedQuery与query的不同** - **managedQuery**:这是一个由`Activity`类提供的方法,用于查询内容提供者(Content...
`ContentResolver`提供了查询、插入、更新和删除数据的方法。 **获取ContentResolver对象:** - 通常情况下,可以通过`Activity`或`Context`的方法`getContentResolver()`来获取`ContentResolver`实例。 **常用...
// 使用 ContentResolver 或 Activity 的 managedQuery 方法查询数据 Uri uri = ContentUris.withAppendedId(People.CONTENT_URI, 5); // 指定 ID 的 URI Cursor cursor = getContentResolver().query(uri, null, ...
这通常涉及到使用`ContentResolver`和`managedQuery()`方法。在示例中,我们查询`MediaStore`来获取图像数据,设置列`MediaStore.Images.Media.DATA`作为查询的目标。`MediaStore.Images.Media.DATA`是图片的本地...
应用程序需要继承ContentProvider类并实现其核心方法,如`query()`、`insert()`、`delete()`和`update()`,以提供对数据的访问。 - **Uri(Uniform Resource Identifier)**:Uri是ContentProvider中用于定位数据...