What is difference between contentprovider and contentResolver in android - 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

Related

Difference between querying with Android Content resolver and Sqlite database connection?

I have a doubt that how Content resolver works, does it uses sqlite database connection implicitly for querying or how does it works and what may be the major difference in using content resolver and sqlite database connection for querying in android?
ContentResolver is used to select the specific ContentProvider.
Content Resolver provides an abstraction from the application’s Content Providers, Content Providers provide an abstraction from the underlying data source (i.e. a SQLite database).
ContentResolver --> ContentProvider --> SQLiteDatabase
I found some decent explanation about Content Resolvers & Content Providers here. It's worth a read. Hope this helps in understanding a bit more about Content Resolvers & Content Providers
Contentprovider/Contentresolver use a modell similar to sql-dabase:
projection: you have colums you are interested in (in sql select col1,col2,....)
selection or filter: find/filter rows (in sql WHERE expression)
Contentprovider can be easily implemented with a sqLite database.
But Contentprovider can also be implemented without any database.
Example: the media contentprovider (photo, video and audio files) is implemented with a sqLight database
Example: the DocumentFile contentprovider for files (in internal memory or on sdcard, on usbstick, in the cloud...) are not implemented through a database but can be queried like any other contentresolver.
*
When to use Contentproviders instead of using the raw database?
If you want to share data with other apps (example the phonebook-conacts contentprovider)
When it is essential that one app keeps control over what other apps can do. Example: Since android-7.0 access to files on sd-card is restricted to make implementing malware more difficuilt. Other apps can use the DocumentFile-provider/resolver

Can we use one database for two or more application

I am about to create two applications which have the same SQLite database. I went through this link. But it does not make sense. I have to update the database from both applications. Please suggest one or more better ways.
You should read about Content Provider
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 SQLiteDatabase.
https://developer.android.com/guide/topics/providers/content-providers
https://developer.android.com/reference/android/content/ContentProvider
Content Provider is a great way to share database with other applications.
When you create a Content Provider, you can define which part of your database can be shared as well.
A Content Provider has following parts-
1. A class that extends ContentProvider class
2. An Authority that distinguishes this ContentProvider from others on the Android O.S.
3. A Contract class that holds Uri to tables and columns that you want to share with other applications
4. A ContentResolver to access data from a ContentProvider
5. Set of user defined permissions to write to and read from a shared ContentProvider. Permissions allow only those apps that you authorize to access your ContentProvider
Here is a simple tutorial about the use of ContentProvider and how to share your database with other applications- https://www.tutorialspoint.com/android/android_content_providers.htm

How to know content provider database column name form another app[Content resolver]

I am new to android. I have one doubt in Content provide and resolver. I have my custom content provider app and i want to access its database from another app.I have created uri and had given authorise in manifest. While inserting new value from another[Content Resolver] how will my resolver app know column names of contentprovider because we have to create ContentValues Object and there column name is required while inserting data... Please help
If the third party app is making their ContentProvider public, they probably have a documentation on how to access it, otherwise you should contact the app's author.
Otherwise, you can use a null projection querying data from the same content provider and print all the columns in the cursor you retrieve.
The standard approach in implementation of ContentProvider is to create additional "contract" class which encapsulates all information needed for communication with the ContentProvider (whether this ContentProvider exported or not).
If your ContentProvider should be accessible from other applications, you export it and publish the respective "contract" class to clients (e.g. publish JavaDoc on website, send by mail, etc.).
Example of "contract" class found in AOSP is ContactsContract class. Its JavaDoc is being published here. Its source can be found 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;

Creating android content provider by "extending" / "not extending" ContentProvider class

Q-1 - To create my own Content Provider class, when I should extend ContentProvider class and when I should not extend ContentProvider class ?
Q-2 - If I create Content Provider without CONTENT_URI(like many other built-in content providers in android.provider.*, how will I use managedQuery(...) or query(....).
I have seen response to similar question in this question on content provider but I am not sure they answer it completly.
1) Even with extension you'd be implementing the methods anyway. A ContentProvider allows you to actually use the internal system for Android to access data from different places in your application. Basically, if you are storing data, extend the ContentProvider and use either ContentResolver.query or Activity.managedQuery to access that data.
2) AFAIK (and this could be wrong), you need a CONTENT_URI when you create a ContentProvider. This is how the ContentResolver knows where it should be pulling from and one way to allow your application to even access that data (through the application manifest). So, use a CONTENT_URI. There isn't much of a reason not to IMO.

Categories

Resources