开发者

managedQuery() vs context.getContentResolver.query() vs android.provider.something.query()

开发者 https://www.devze.com 2022-12-26 15:07 出处:网络
Pretty simple. What is the d开发者_如何学JAVAifference among those three? I want to list every Image in a device.

Pretty simple. What is the d开发者_如何学JAVAifference among those three?

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


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

Update:

managedQuery is now deprecated (as of Android 3.0).


managedQuery(..) is now deprecated (as of Android 3.0). Watch out..

Android error: java.lang.IllegalStateException: trying to requery an already closed cursor

0

精彩评论

暂无评论...
验证码 换一张
取 消