In our previous Android tutorials for beginners, we discussed the first three building blocks of Android: Activity, Intent and Services. This article is fourth in this series of Android Tutorials for beginners and discusses Content Provider, which is another very important component of the Android System.

Content Provider is your Android system’s middleman, facilitating data interchange between different Android applications. Let us get a little into the details of that in this Android tutorial.

Android Tutorials: Content Provider

Content Provider facilitates access to a central data store or warehouse to allow data sharing and data manipulation across different applications.

Here are a few examples of default Content Providers in Android system’s API:

These content providers allow the user abstraction from an underlying database. Generally, Content Providers use SQLite to store underlying database.

Let us take the ever-helpful example of the good old Facebook application, that has helped us simplify things in previous Android Tutorials as well!

All of you have at some point in your social networking life, uploaded a pic to your Facebook timeline, right! How do you do it?

On clicking the Photo Button on the wall, you get to the photo gallery. From there you can choose a photo to upload.

Having read the previous articles of Android Tutorials series, you know about Activities and Intents; therefore, you know that this is what happens:

Your Facebook Wall is an “ACTIVITY”. As you click on the Photo Button, an “INTENT” is passed, that conveys the message and the “CONTENT PROVIDER” (Photo Gallery) opens. The photo is uploaded using a network upload “SERVICE”.

Watch this live video to know how Content Provider works inside your Facebook Application.

Why does Android System need Content Providers?

The need for Content Providers arises because the database created in one application is not visible to a second application.

It is simple to create and store database in different applications using SQLite, however the problem is this, a database in Android is private to the application that creates it. There is no common storage area in Android that every application can access. Therefore, for different applications to use a database, Android system needs an interface that allows such inter-application and inter-process data exchange. This is where content provider comes to play.

Do I really need a Content Provider?

1) You do not need to develop your own provider if you want a private database for a particular application (this database would not be accessible to applications other than the one that created it).

3) You would also need a Content Provider to copy and paste complex data from your application to other applications.

What are the operations supported by a Content Provider?

Content providers support the following basic operations:

1) Querying: Queries the Content Provider for all the objects, based on the specified URI.

2) Delete: Deletes the specified objects from the database of a Content Provider.

3) Update: Makes updates to the objects in the database.

4) Insert: Inserts new object to the database.

Steps to carry out an operation in Content Provider

Step 1: Accessing a Content Provider

ContentResolver client object is used to access data from Content Provider. It communicates with provider object, which in turn accepts the request to access data and returns the desired results. The data exchange interface provided by the provider and provider client object allows communication across different processes/applications.

The application that needs to access the database has to declare this and request permissions in its manifest file. This would be discussed in detail in our subsequent Android tutorials.

Content URI

Content URI is one of the arguments used to identify the data in a provider. It has four parts:

1) Scheme: The scheme for content provider has a constant value: “content”.

2) Authority: It is the symbolic name of the provider, and is unique for each one. This is how we single out the desired content provider from a list of so many.

3) Path: Path helps distinguish the required data from the complete database. For instance, the Call Log Content Provider differentiates between Missed Calls, Received calls etc. using different paths.

4) ID: It is not a mandatory component, and may not be present in the URI; but if present, it should be numeric. For instance, if you want to access a specific music file from your Media Content Provider, you would specify an ID as well.

The process

Using the provider authority, the ContentResolver identifies the correct content provider (as authority is unique for each content provider). Having done that, the path component of URI is used to select the correct (requested) data table. In case an ID is present, the provider would know what exact data is being requested.

URI’s are of two types:

Additionally, URIs can have limiting information as well.

Step 2: How to retrieve data from a Content Provider

Even though the ContentResolver has access to the data table now, it cannot retrieve the required data unless the application has “read access permission” for that particular provider. This permission is defined in the manifest file of each content provider.

All that an application (that wants to access this database) has to do is to request this permission.

Now as discussed earlier in this Android tutorial, four operations can be carried out using a content provider. We will go over each one by one.

QUERYING

Now, you have accessed the provider, and have permission to retrieve data from it. The next step is to construct the query to request the required action from the provider.

Here are the arguments used while querying:

1) URI: It works exactly as explained above.

2) Projection: The query should return a set of columns from the entire database table. This is known as projection. Passing null will return all columns, which is inefficient.

3) Selection Clause: A filter declaring which rows to return, formatted as an SQL WHERE clause (excluding the WHERE itself). Passing null will return all rows for the given URI.

For instance, if you enter an alphabet (say ‘P’) in the search column of your address book, then it would return all the contact details starting with ‘P’. However, if you do not enter anything in the search bar, the complete list of contacts is retrieved (the selection clause is set to ‘null’ in such cases).

4) Selection Argument: You may include “?s” in selection, which will be replaced by the values from selectionArgs, in the order that they appear in the selection.

5) SortOrder: SQL ORDER BY clause (excluding the ORDER BY itself). Passing null will fetch the results which may be unordered.

INSERTION

Let us suppose you want to insert new contacts to your address book. ContentValues object is used to do these insertions. The ContentValue object keys and the Content Provider columns must match to achieve this. Here’s an example for this:

CODE EXAMPLE FOR Insertion :

The operation is to insert new entry with name “Rajnikant” and number “9988999888”

There is some error in Downloading Code :( Admin… I want source code urgently… Please help

Anuj

Nice Tut! :-) But I am unable to Download Source code… I need it.
How to Add And Retrive words from UserDictionary? Please Help…

ricejoldell

Marvelous Post.thanks for share..

slubjellelofs

Aw, this was a really nice post. In idea I would like to put in writing like this moreover – taking time and actual effort to create a rather great article?- but what can I say?- I procrastinate alot and by no means appear to obtain some thing performed.