I watched a course about ContentProvider
on the Internet demonstrating how to define and use a ContentProvider
.
I was confused about using the method named getContentResolver()
. What does this method return?
My ContentProvider
is not instanced and the code just writes that getContentProvider().query()
.
I don't understand how ContentProvider
works.
They enable you to decouple your application layers from the underlying data layers, making your application data-source agnostic by abstracting the underlying data source. Show activity on this post.
To access the data from a content provider, URI is used as a query string. Details of different parts of Content URI: content:// – Mandatory part of the URI as it represents that the given URI is a Content URI. authority – Signifies the name of the content provider like contacts, browser, etc.
After creating the content provider , specify the content provider in the manifest file. You can mention content provider using the tag. Inside the provider tag dont forget to mention the name and authorities attributes. This declaration should be ..
It returns Content Resolver.
What is the Content Resolver?
The Content Resolver is the single, global instance in your application that provides access to your (and other applications') content providers. The Content Resolver behaves exactly as its name implies: it accepts requests from clients, and resolves these requests by directing them to the content provider with a distinct authority. To do this, the Content Resolver stores a mapping from authorities to Content Providers. This design is important, as it allows a simple and secure means of accessing other applications' Content Providers.
The Content Resolver includes the CRUD (create, read, update, delete) methods corresponding to the abstract methods (insert, delete, query, update) in the Content Provider class. The Content Resolver does not know the implementation of the Content Providers it is interacting with (nor does it need to know); each method is passed an URI that specifies the Content Provider to interact with.
What is the Content Provider?
Whereas the Content Resolver provides an abstraction from the application's Content Providers, Content Providers provides an abstraction from the underlying data source (i.e. a SQLite database
). They provide mechanisms for defining data security (i.e. by enforcing read/write permissions) and offer a standard interface that connects data in one process with code running in another process.
Content Providers provide an interface for publishing and consuming data, based around a simple URI addressing model using the content:// schema
. They enable you to decouple your application layers from the underlying data layers, making your application data-source agnostic by abstracting the underlying data source.
Source - androiddesignpatterns
getContentResolver () return a ContentResolver instance for your application's package.
Pasting it from developer.android.com
Content providers manage access to a structured set of data. They encapsulate the data, and provide mechanisms for defining data security. Content providers are the standard interface that connects data in one process with code running in another process.
When you want to access data in a content provider, you use the ContentResolver object in your application's Context to communicate with the provider as a client. The ContentResolver object communicates with the provider object, an instance of a class that implements ContentProvider. The provider object receives data requests from clients, performs the requested action, and returns the results.
http://developer.android.com/guide/topics/providers/content-providers.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With