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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2595213/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:28:39  来源:igfitidea点击:

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

android

提问by Macarse

Pretty simple. What is the difference 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(),android.provider.MediaStore.Images.Media.query()还是context.getContentResolver.query()

回答by Pentium10

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

managedQuery()将使用 ContentResolver 的 query()。不同之处在于managedQuery()活动将保留对您的 Cursor 的引用并在需要时关闭它(onDestroy()例如)。如果您query()自己做,您不得不将 Cursor 作为敏感资源进行管理。例如,如果你忘记了close()onDestroy(),你将泄漏底层资源(logcat 会警告你。)

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()方法或Activity.managedQuery()方法。这两种方法都采用相同的参数集,并且都返回一个 Cursor 对象。但是,managedQuery()导致活动管理光标的生命周期。托管 Cursor 处理所有细节,例如在 Activity 暂停时卸载自身,以及在 Activity 重新启动时重新查询自身。您可以通过调用 来让 Activity 开始为您管理非托管 Cursor 对象Activity.startManagingCursor()

Update:

更新:

managedQueryis now deprecated (as of Android 3.0).

managedQuery现在已弃用(从 Android 3.0 开始)。

回答by user979247

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

managedQuery(..) 现在已弃用(从 Android 3.0 开始)。小心..

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

Android 错误:java.lang.IllegalStateException:试图重新查询已关闭的游标