How does getContentResolver() work?

Huang Jie picture Huang Jie · Jul 10, 2013 · Viewed 29.5k times · Source

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.

Answer

Aditya picture Aditya · Jul 10, 2013

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