Android Content of Providers

Android provides a neat way of sharing data between applications: Content Providers. System data sources such as the address book, the SMS database and calendar are all exposed via Content of Providers and any application may publish a Content Provider. The great benefit of Content Providers is that they provide a consistent mechanism for publishing and consuming data across the entire Android platform regardless of the underlying data structure.

Content Providers expose data as a table or grid structure in the form of a Cursor which allows navigation through a set of results in a similar manner to the JDBC cursor traditionally used to access data in a Java application. Content providers are accessed via a ContentResolver object which provides access to any ContentProvider. The signature for the query method reveals a number of interesting things:

public Cursor query(Uri uri,
                     String[] projection,
                     String selection,
                     String[] selectionArgs,
                     String sortOrder)


The first parameter is a Uri which is the thing which actually identifies the ContentProvider. Provider Uris are declared in the manifest of the application which hosts it. They consist of three components: the protocol identifier which is always content, an authority which is the overall identifier for an individual ContentProvider and a path which identifies the data within a content provider. You can think of the path component as being a table name within a database but as we shall see, you can do more with it than that.
The other parameters of the query method are a projection which is an array containing the names of the columns to select from the content provider.
The remaining parameters will be familiar to those used to working with SQL query statements: The selection and selectionArgs combined form the where clause for the query.
sortOrder is the equivalent of the ORDER By clause of a SQL select statement.

Once you understand the basics, working with a Content Provider is straight-forward. In the example below we are accessing the flights data from a Content Provider with an authority of com.ltree.flysecure.flightcp. The code performs the following steps:

  1. Obtain a reference to a ContentResolver
  2. Create a Uri object from a String representation of the Content Provider Uri
  3. Define a projection to specify the columns to return from the provider
  4. Build a parameterized SQL query
  5. Perform the query
  6. Retrieve the values from the cursor
ContentResolver cr = getContentResolver();
Uri uri = Uri.parse("content://com.ltree.flysecure.flightcp/flights");
String[] projection = {
String queryString = "origin=? AND destination=?";
String[] queryArgs = {"LHR", "IAD"};
String orderBy = "FlightNum";
Cursor cursor = cr.query(uri,

      // Load values from the cursor
    String flightNum = cursor.getString(1);
    String origin = cursor.getString(2);

For those familiar with traditional JDBC cursor processing watch out for a couple of differences at this point. First, the index value in the getXXX() methods is 0 based not 1 based. Secondly, the column positions are defined by the projection passed into the query method and are independent of the underlying storage.

The simplistic method of accessing the cursor shown above is fine as an example. There are other, more powerful ways of using the cursor such as directly associating it with a list.We cover those in Learning Tree’s course on the topic: Developing Applications for Android Devices – Hands On

Type to search

Do you mean "" ?

Sorry, no results were found for your query.

Please check your spelling and try your search again.