How does getContentResolver() work? - android

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.

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

Related

why don't we directly call methods on a Content Provider object?

Android development:
I don't understand why we use Content resolver to access Content provider . Instead why don't create an instance of Content Provider and directly call methods on it ?
Technically you could do this if ContentResolver would be built differently by android.
But the reason to this it's not technical, it's arcitcherual.
content providers are primarily intended to be used by other
applications, which access the provider using a provider client objects
When client application reaches other application data - for example Whatsapp automatically add your contacts via the contact application ContentProvider they don't, and don't need to, care where the ContentResolver gathered the data from (e.g local SQlite or maybe a web server).
If one app would directly reaches other app's data many API's would be "broken" over version updates.
Great guide on how the implementation actually happens in the code here.

Contentprovider: How to use a reference for it?

I want to use a Contentprovider within a Service. I've written a handler-class to parse JSON and want to insert data in my contentprovider, how is it possible to give a Service a reference for the contentprovider?
The comments are on the right track, but I feel that they need elaboration:
Content providers, even ones that you only use within your own app, are not designed to be referenced directly. Instead, an app should use methods in ContentResolver to access a content provider. ContentResolver features the same set of methods as those that you must implement for ContentProvider (its abstract methods). For example, if you want to insert data into a ContentProvider, you call getContentResolver().insert(). A ContentResolver is a general-purpose object for communicating with content providers, file providers, and the sync adapter framework.
When you define a content provider in your app, you have to define its authority value in the manifest, and you also have to write code that interprets incoming content URIs. The best way to work with content URIs is to start by assigning a separate content URI to each table in your provider, using the form
content://*authority*/*path*
Authority is the authority string you specified in the manifest; the de facto standard is to use your app's package name with ".provider" appended to it. Path is a descriptive term for the table it represents. For example:
content://com.example.android.myapp.provider/sales
to point to the Sales table in your provider. You can get fancier if you want, but most developers will satisfy their needs with this proposal.
To work with content URIs in your content provider, use the android.content.UriMatcher class.
Just call getContentResolver() from your service;

What is difference between contentprovider and contentResolver in android

What is the difference between ContentProviders and ContentResolver? I do not want for the SQLite database. I am developing an application for media.
I found some explanation here. In summary
Content Resolver resolves a URI to a specific Content provider.
Content Provider provides an interface to query content.
The way to query a content provider is contentResolverInstance.query(URI,.....)
ContentProviders are used to abstract the database from other parts and acts as an interface between your database and UI/other classes. You must create your own ContentProvider to share your app data with other apps.
ContentResolver is used to select the right ContentProvider based on the ContentUris. A ContentUri may look like
content://com.android.contacts/contacts/3
content:// is called scheme and indicates that it is a ContentUri.
com.android.contacts is called Content authority and ContentResolver uses it to resolve to a unique provider (in this case, ContactProvider).
contacts is the path that identify some subset of the provider's data (for example, Table name).
3 is the id used to uniquely identify a row within the subset of data.
NOTE : Your own app can also use this route to handle its data.
See Content Providers in Android for more detail
Two layered Abstraction :
ContentResolver --> ContentProvider -->SQLiteDatabase
The main difference is this as mentioned in other answers.
ContentProvider exposes private data of your application to external application
while
ContentResolver provides the right ContentProvider among all the ContentProviders using a URI.
Deeper Understanding (of two-layered abstraction)
Let's take a detour.
We all know that when we create an SQLite database then the database remains private to your application which means, you just can not share your app data with any other external application.
How data is shared then?
ContentProvider and ContentResolver are part of android.content package. These two classes work together to provide robust, secure data sharing model among applications.
ContentProvider exposes data stored in the SQLite database to other application without telling them the underlying implementation of your database.
So it abstracts the SQliteDatabase. But wait there is a catch !!!
The external application can not directly access ContentProvider. For that, you need to first interact with another class called ContentResolver
Think ContentResolver as a ContentProvider finder. There is only one instance of it and all the ContentProviders of your Device are registered with a simple Namespace URI. If you want to reach a particular ContentProvider you just need to know its URI. Pass it to ContentResolver and it will find the Provider using the URI.
Now lets have a look at the most important method getContentResolver().query(URI,String[] proj.....)
What happens when getContentResolver().query(URI,String[] proj.....) gets called
query() method belongs to ContentResolver class however it invokes the abstract query() method of resolved ContentProvider and returns Cursor object.
In this way, the External application gets exposed to the private database via two abstraction layers.
Just to add more points
You cannot create your own ContentResolver class but you can always create your own
ContentProvider class
Hope you have a better understanding
You can also see some sample code here for creating SQLitedatabase, ContentProvider etc, But it's not well documented.
In 2021 :D
Content Resolver : For Data Request
Content Provider : For Data Response

Can I use a content resolver in my application without creating a provider?

I am reading the developer guide about the content providers and as it's mentioned there that I shouldn't create my own provider unless there is a need to.
So my questions are:
Can I use content resolver in my application without creating my own provider?
What is the best approach? should I use always a content resolver (and create my own provider if necessary) to do all the operations with the DB or talking to SQLiteHelper class directly.
Can I use content resolver in my application without creating my own provider?
You can use a ContentResolver with any other existing ContentProvider, such as those supplied by the OS (e.g., ContactsContract).
should I use always a content resolver (and create my own provider if necessary) to do all the operations with the DB or talking to SQLiteHelper class directly
There are differing opinions on that subject. Personally, I am not a big fan of ContentProvider, and so I only use it when I need to, such as for inter-process communication. However, there are those who think that using ContentProvider purely for internal use within an app is wonderful. There is no right or wrong answer.

How does ContentProvider work internally

Everyone,I was looking at android source.But I could not find something valuable for me.Actually,I want to know whether ContentProvider is worked like AIDL.You know ,AIDL can achieve the communication between two independence application.
So, I just want to see how does the ContentProvider work internally.
From ContentProvider Source code
Content providers are one of the primary building blocks of Android
applications, providing content to applications. They encapsulate data
and provide it to applications through the single ContentResolver
interface. A content provider is only required if you need to share
data between multiple applications. For example, the contacts data is
used by multiple applications and must be stored in a content
provider. If you don't need to share data amongst multiple
applications you can use a database directly via
android.database.sqlite.SQLiteDatabase.
According to this and to the information in the link I sent you, ContentProvider isn't using AIDL... It is using the ContentResolver interface.
Content Providers use IPC Binders internally. "In fact, Intents and ContentProvider are just a higher-level abstraction of Binder"

Categories

Resources