Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does getContentResolver() work?

Tags:

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.

like image 978
krosshj Avatar asked Jul 10 '13 09:07

krosshj


People also ask

What is the use of getContentResolver ()?

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.

How do you read data from a content provider?

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.

How can I access my content provider from another application?

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


2 Answers

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

like image 175
Aditya Avatar answered Oct 14 '22 02:10

Aditya


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

like image 35
GyaniPundit Avatar answered Oct 14 '22 04:10

GyaniPundit