Steps for creating a Content Provider

Paul Núñez
5 min readNov 7, 2017

--

Meanwhile I study for the Android Developer Certification, I decided to document briefly the series of steps for setting up a Content Provider. I usually haven’t had the need to use it, but it is a handy tool to have in our utility belt, and as it is one of the core Android components, something we should be familiarized with.

I have to say that this isn’t exactly a tutorial for creating it, here I just show the major points. For more details you could check Codepath’s guide for creating it, or other greats tutorials in the web.

Before we start, we need to have a few definitions fresh:

  • URI (Uniform Resource Identifier):
    Are used to identify or give the location of some content or data. IE: URL’s.
  • MIME types:
    These are the strings that describe the type of data stored at the input URI. This is also now as the Content-Type. A good example for this functionality is when you’re sending an implicit intent with an URI on the data field, the system will check the MIME type of that URI to determine which app component on the device can handle the request.

Steps for creating a Content Provider

1. Create a table contract:

In this class will be declared all the information associated with the table, such as: the content authority, the path to the items, the item entry class and the SQL sentences, more about these at the bottom.

Product table contract.
  • Content Authority: A name for the entire Content Provider, that will help us locate it. This similar to the relationship between a domain name and its website. It is a common behavior to use the top package of your app.
Declaring the content authority.
  • Item path: Possible path (appended to base content URI for possible URI’s). For instance, content://com.example.android.products/products/ is a valid path for looking at product data. content://com.example.android.product/staff/ will fail, as the ContentProvider hasn’t been given any information on what to do with “staff”.
Item path to be appended to the base content URI.
  • Item entry class: This class will define all table contents and should implement the BaseColumns interface. This interface only adds two static Strings variables to use as columns. These variables are: ID for the “_id” and COUNT for “_count” columns.

We will create this as an inner class of ProductContract. The contents we will be defining on this class are:

  1. The content URI
  2. The MIME for being able to know if we are working with a list or a single item
  3. Table name
  4. Column names
  5. Relations
Product entry values.
  • SQL create and delete table statements:
SQL for creating and delete the products table.

2. Setting up the Database helper:

For this Android provides a class named SQLiteOpenHelper, and as the documentation says, this class will manage the database creation and versioning.

This step is very straight forward, we just have to extend the SQLiteOpenHelper, pass the database name and version to the super class and override the onCreate and onUpgrade methods:

ProductDbHelper class.

For the onUpgrade method, depending of the use case, you may have to handle the migration so all the user data is not deleted.

3. Setting up de Content Provider:

  • Create a class that extends from ContentProvider and implement the corresponding methods. All the code presented on this step will be inside this class.
  • Declare URI’s matcher codes for each type of item:
    These are used along with the UriMatcher to create a URI pattern to help us “choose the action we are going to make for an incoming content URI”, instead of returning the whole content URI it will return integer values that we declare by ourselves. These are random but must be different and unique.
URI matcher codes.
  • Instantiate the URI matcher:
URI matcher.
  • Now create the content URIs:
Content URIs.
  • In the getType method set the URI matchers to return the correct MIME:
The getType method of the ProductProvider class.
  • Implement the CRUD methods: In these we can check, with the URI matcher, the type of content the user is requesting, in this case if is one product or many. Lets see an implementation for the query method:
Query method of the ProductProvider class.

4. Declare it on the manifest:

Declaring the content provider in the manifest.xml.

And those are the basic steps for creating a content provider!

--

--

Paul Núñez

Trying to ship better code a Bit at a time @Pinger. Mostly write about Android development. Dominican 🇩🇴🌴.