Home > Android > Android Content Providers

Android Content Providers

Content providers store and retrieve data and make it accessible to all applications.
If you want to make your own data public, you have two options: You can create your own content provider (a ContentProvider subclass) or you can add the data to an existing provider.
All Services must be declared in the manifest file.

Content Provider Basics

* All content providers implement a common interface for querying the provider and returning results as well as for adding, altering, and deleting data.
* It’s an interface that clients use indirectly, most generally through ContentResolver objects.
* ContentResolver cr = getContentResolver();
* You can then use the ContentResolver’s methods to interact with whatever content providers you’re interested in.
* The system instantiates all ContentProvider objects.There is a single instance of each ContentProvider object.

Here are some of Android’s most useful built-in content providers Content Provider :-

. Intended Data
. Contacts : Contact details
. Browser : Browser bookmarks, browser history, etc.
. CallLog : Missed calls, call details, etc. You can use the ContentResolver’s methods to interact with whatever content providers you’re interested in.
. MediaStore : Media files such as audio, video and images
. Settings : Device settings and preferences

The Data Model

Content providers expose their data as a simple table on a database model, where each row is a record and each column is data of a particular type and meaning.
It’s an interface that clients use indirectly, most generally through ContentResolver objects.

content provider

URIs

. Each content provider exposes a public URI (wrapped as a Uri object) that uniquely identifies its data set.
. A content provider that controls multiple data sets (multiple tables) exposes a separate URI for each one.
. All URIs for providers begin with the string “content://”.
. android.provider.Contacts.Phones.CONTENT_URI
. android.provider.Contacts.Photos.CONTENT_URI
. The URI constant is used in all interactions with the content provider.
. Every ContentResolver method takes the URI as its first argument.
. It’s what identifies which provider the ContentResolver should talk to and which table of the provider is being targeted. Every ContentResolver method takes the URI as its first argument.

Creating Content Provider

To create a content provider, you must:
. Set up a system for storing the data. Most content providers store their data using Android’s file storage methods or SQLite databases.
. Extend the ContentProvider class to provide access to the data.
. Declare the content provider in the manifest file for your application (AndroidManifest.xml).

Extending the ContentProvider class

You define a ContentProvider subclass to expose your data to others using the conventions expected by ContentResolver and Cursor objects
Implement six abstract methods declared in the ContentProvider class:
. query() – must return a Cursor object that can iterate over the requested data.
. insert()
. update()
. update()
. getType()
. onCreate()
Because these ContentProvider methods can be called from various ContentResolver objects in different processes and threads, they must be implemented in a thread-safe manner

Creating Content Provider : Additionals

Define a public static final Uri named CONTENT_URI.
public static final Uri CONTENT_URI = Uri.parse(“content://com.example.codelab.transporationprovider”);
If the provider has subtables, also define CONTENT_URI constants for each of the subtables.

content://com.example.codelab.transporationprovider/air/domestic
content://com.example.codelab.transporationprovider/air/international

Define the column names that the content provider will return to clients.
Be sure to include an integer column named “_id” (with the constant _ID) for the IDs of the records.
. If you’re using the SQLite database, the _ID field should be the following type: INTEGER PRIMARY KEY AUTOINCREMENT.