managedQuery()与getContentResolver().query()的区别:
Romain Guy写道:
http://groups.google.com/group/android-developers/browse_thread/thread/9c887ac024b36798
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.)
mangedQuery会实际查询的工作还是调用ContentResolver的query这一点与getContentResolver一样,但managedQuery会让Activtiy来帮你管理你的cursor的,而不用你自己去管理。
分享到:
相关推荐
`getContentResolver().query()`则用于查询与URI关联的Cursor,从中我们可以获取到图片的本地路径,并将其显示在TextView上。 总结起来,在Android 4.4及更高版本中获取相册图片和路径的关键在于适配新的API,如`...
Cursor cursor = getContentResolver().query(uri, null, null, null, null); // 查询联系人信息并按名字升序排序 String[] projection = new String[] { People._ID, People._COUNT, People.NAME, People....
Cursor cursor = getContentResolver().query( Uri uri, // 要查询的数据源 String[] projection, // 需要返回的列 String selection, // 选择条件 String[] selectionArgs, // 选择参数 String sortOrder // ...
5. **优化与性能**:为了提高性能,ContentProvider应尽量减少不必要的数据操作,避免在`query()`、`insert()`等方法中执行耗时的操作。使用`CursorLoader`或`AsyncTask`可以帮助异步加载数据,避免阻塞UI线程。 6....
1. 获取ContentResolver:通过Activity或Context的getContentResolver()方法获取ContentResolver对象,这是与ContentProvider交互的主要工具。 2. 数据操作:利用ContentResolver执行对数据的操作,包括: - 查询...
查询操作通常通过 `ContentResolver.query()` 或 `Activity.managedQuery()` 方法执行。后者除了返回 `Cursor` 对象外,还会自动管理 `Cursor` 的生命周期,例如在 Activity 暂停或销毁时关闭 `Cursor`,以防止资源...
cursor = mContext.getContentResolver().query(uri, null, null, null, null); if (cursor != null) { cursor.moveToFirst(); // do something } } catch (Exception e) { e.printStackTrace(); } finally ...