Pro Android with Kotlin
CHAPTER 6: Content Providers
Download 5.42 Mb. Pdf ko'rish
|
@de android telegram Pro Android with Kotlin Developing Modern Mobile
79
CHAPTER 6: Content Providers Here again, you must use the close() method on the stream once its work is done; only the AssetFileDescriptor gets closed for you automatically when you close the streams. Informing Listeners of Data Changes A client addressing a content provider via its ContentResolver field (e.g., Activity. contentResolver) can register to be notified of content changes by calling the following: val uri = ... // a content uri contentResolver.registerContentObserver(uri, true, object : ContentObserver(null) { override fun onChange(selfChange: Boolean) { // do s.th. } override fun onChange(selfChange: Boolean, uri: Uri?) { // do s.th. } } ) The second argument to registerContentObserver() specifies whether sub-URIs (the URI plus any other path elements) will lead to a notification as well. The constructor argument to ContentObserver can also be a Handler object for receiving onChange messages in a different thread. For this to work, on the content provider side you may need to take care that the event gets correctly emitted. For example, inside any data modification method, you should add the following: context.contentResolver.notifyChange(uri, null) Also, to make change listening bullet-proof, you might want to inform any Cursor objects returned by the query() methods. For this aim, a cursor has a registerContentObserver() method that you can use to collect cursor-based content observers. The content provider may then send messages to those content observers as well. Extending a Content Provider We have seen that a content provider allows for accessing database-like content and files. If you don’t like the way this is done too much or have your own ideas about what a content provider should be able to do, you can implement the call() method as follows: override call(method:String, arg:String, extras:Bundle): Bundle { super.call(method, arg, extras) // do your own stuff... } |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling