managedQuery()与context.getContentResolver.query()与android.provider.something.query()getContentResolv

2023-09-14 00:07:21 作者:西瓜。

pretty的简单。 这三者之间的区别是什么?

Pretty simple. What is the difference among those three?

我要列出每个图像的装置。 我应该使用 managedQuery() android.provider.MediaStore.Images.Media.query() context.getContentResolver.query()

I want to list every Image in a device. Should I use managedQuery(), android.provider.MediaStore.Images.Media.query() or context.getContentResolver.query()

推荐答案

managedQuery()将使用ContentResolver的的查询()。所不同的是 与 managedQuery()的活动将继续引用您 光标和关闭它在需要时(在的onDestroy()的实例。)如果 您查询()你自己,你的会要管理光标作为 敏感的资源。如果你忘记了,例如,以关闭()的onDestroy(),你会泄漏底层资源(logcat中会警告你 这件事。)

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.)

要查询的内容提供商,您可以使用该 ContentResolver.query()法或 Activity.managedQuery()方法。这两种方法都相同的一组参数,并且都返回一个Cursor对象。然而, managedQuery()使管理光标的生命周期的活动。被管理的游标处理所有细微之处,如卸本身当活动暂停,并重新查询自己的活动重新启动时。你可以问一个活动通过调用开始为你管理一个非托管Cursor对象 Activity.startManagingCursor()

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().

更新:

managedQuery 现在去precated(由于Android 3.0)。

managedQuery is now deprecated (as of Android 3.0).