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.
Related
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
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
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.
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"
dear i am confused the use of content provider in android applications.i go through various sides and read it but i am still confused about content provider ..can any one explain it in some simple way ,how to use it?
Content providers store and retrieve data and make it accessible to all applications....whats that means?
can it means any package name or in same pakage name...
if i am making another app of diffrent package name then that c.p. is available also in that package?
OK, first of all, a content provider will be used if and only if you want to use the same data through several applications. You can still use it if you want to implement it in your application.
Now, by definition a ContentProvider "Content providers manage access to a structured set of data. They encapsulate the data, and provide mechanisms for defining data security." (According to Android Developer Guides)
So in other words, it's like a new layer of abstraction that you are adding to your data, assigning a new man-in-the-middle that will request everything you ask it to do (read, write, update your database, files, or webservices). And this man-in-the-middle is not only available for you, it's also available for other apps if you set the tag android:exported to true in the <provider> in your manifest file.
A ContentProvider needs you to implement:
A contract: Basically a class with public static variables with your tables names and the fields of info for those tables.
A data model: Normally a database in SQLite but you can implement it also with third party libraries like SugarORM, GreenDao, Retrofit, local files, etc).
Your custom ContentProvider: Extend ContentProvider and inside here set the calls to your data model.
This is a good tutorial on how to make your own ContentProvider:
http://www.grokkingandroid.com/android-tutorial-writing-your-own-content-provider/
There are also a few libraries that will help you create a ContentProvider automatically (I find it amazing because it takes you like 10 min to set up everything, instead of creating your own ContentProvider from scratch which could take you days).
My personal favorite is Schematic:
https://github.com/SimonVT/schematic
I hope it helps. Kind regards!
how to use it?
you actually don't use it directly, but via the content resolver (which will, according to the URI you give it, query the right ContentProvider) :
getContentResolver().query("content://com.myapp.myprovider/data/", ...);
will find your content provider if that one is registered to handle URIs that match "content://com.myapp.myprovider/data/"
if i am making another app of diffrent
package name then that c.p. is
available also in that package?
If you decide to publish the content provider, it is available outside of your application (this is a setting in the manifest).
what is the major benifit of using c.p.?
It is a common design pattern in Android to offer access to data. Major benefit is that you can abstract access to your data and decide whether to open it to other applications or not. For instance, without content providers, you could not access the media stored on the phone or the contacts of the phone.
Since a package is meant as a unique identifier for an application, how can you create an app with the same package name? If this was your question, I think I've answered it. If you don't understand something else - feel free to ask. Good luck!
P.S. Consider accepting some answers, your accept ratio is low.