You are on page 1of 10

Definition: Content Providers

A Content Provider is one of the android building blocks which provides the data
across the boundaries of different applications.

Why Content Providers:

PROCESSES PROCESSES PROCESSES PROCESSES


PERMISSIONS PERMISSIONS PERMISSIONS PERMISSIONS
PROCESSES PROCESSES PROCESSES PROCESSES
PERMISSIONS PERMISSIONS PERMISSIONS PERMISSIONS

CONTENT
PROVIDER
ContentProvider used to get data from central repository. Android application contains
content provider to provide data to other applications.

Custom Content provider can be created to get data from database / sdcard / media etc.

Content providers create an abstraction layer between its repository of data and external
application that are using data.

External Application can call Content Provider methods with the use of ContentResolver.

Content provider show data to content resolver as one or many tables that will show
same as relational database.

Android provide number of content providers that store common data such as contact
informations, calendar information, and media files etc.
Purpose Behind Content Providers
• Content providers lets the user centralize content in one place and have
many different applications access it as needed
• Each and every app, in the phone has its own content provider interface
which facilitates the data of one app, can be used by another app based
on necessity
Methods:
1. insert()
2. Update()
3. Delete()
4. Query()
Creating a Content Provider
To create a content provider----
1. Create a new Java class that subclasses the system’s
ContentProvider class.
2. Declare your CONTENT_URI.
3. Implement all the unimplemented methods, such as
insert(), update(), delete(), query(), getID(), and
getType().
4. Declare your content provider in the
AndroidManifest.xml file.
URI ( Uniform Resource Indicator)
1. Objects within a single app shares an address space
2. But Objects with different apps’ cannot know the
address spaces for references of their names.
3. Android uses a mechanism called
Uniform Resource Indicator.
URI – Is a string that identifies a specific resource
for an app, through the content provider
Parts of URI
Contd.,
Part A, content://, is always set to this value. This is written in stone.
Part B, com.marakana.android.yamba.StatusProvider, is the so-called
authority.
It is typically the name of the class, all in lowercase. This authority
must match the authority that we specify for this provider when we
later declare it in the manifest file.
Part C, status, indicates the type of data that this particular provider
provides. It could contain any number of segments separated with a
slash, including none at all.
Part D, 47, is an optional ID for the specific item that we are
referencing. If not set, the URI will represent the entire set. Number
47 is an arbitrary number picked for this example.
Creating a Content Provider
This involves number of simple steps to create your own content provider.
1. Create a Content Provider class that extends the ContentProviderbaseclass.
2. Define the content provider URI address which will be used to access the
content.
3. Create the database to keep the content.
4. Implement Content Provider queries to perform different database specific
operations.
5. Register the Content Provider in the activity file using <provider> tag.

You might also like