Content providers manage access to a structured set of data. They encapsulate the data, and provide mechanisms for defining data security. Content providers are the standard interface that connects data in one process with code running in another process.
When you want to access data in a content provider, you use the
ContentResolver object in your application's
Context to communicate with the provider as a client. The
ContentResolver object communicates with the provider object, an instance of a class that implements
ContentProvider. The provider object receives data requests from clients, performs the requested action, and returns the results.
You don't need to develop your own provider if you don't intend to share your data with other applications. However, you do need your own provider to provide custom search suggestions in your own application. You also need your own provider if you want to copy and paste complex data or files from your application to other applications.
Android itself includes content providers that manage data such as audio, video, images, and personal contact information. You can see some of them listed in the reference documentation for the
android.provider package. With some restrictions, these providers are accessible to any Android application.
Implementing a basic content provider class
1) Create a Contract Class
A contract class defines constants that help applications work with the content URIs, column names, intent actions, and other features of a content provider. Contract classes are not included automatically with a provider; the provider's developer has to define them and then make them available to other developers.
A provider usually has a single authority, which serves as its Android-internal name. To avoid conflicts with other providers, use a unique content authority. Because this recommendation is also true for Android package names, you can define your provider authority as an extension of the name of the package containing the provider. For example, if your Android package name is
com.example.appname, you should give your provider the authority
A content URI is a URI that identifies data in a provider. Content URIs include the symbolic name of the entire provider (its authority) and a name that points to a table or file (a path). The optional id part points to an individual row in a table. Every data access method of ContentProvider has a content URI as an argument; this allows you to determine the table, row, or file to access. Define these in the contract class.
2) Create the Helper Class
A helper class manages database creation and version management.
3) Create a class that extends ContentProvider class
A UriMatcher maps an authority and path to an integer value. The method
match() returns a unique integer value for a URI (it can be any arbitrary number, as long as it's unique). A switch statement chooses between querying the entire table, and querying for a single record. Our UriMatcher returns 100 if the URI is the Content URI of Table and 101 if the URI points to a specific row within that table. You can use the
# wildcard to match with any number and
* to match with any string.
IMPORTANT: the ordering of
addURI() calls matters! The UriMatcher will look in sequential order from first added to last. Since wildcards like
* are greedy, you will need to make sure that you have ordered your URIs correctly. For example:
is the proper ordering, since the matcher will look for
/example first before resorting to the
/* match. If these method calls were reversed and you called
uriMatcher.match("/example"), then the UriMatcher will stop looking for matches once it encounters the
/* path and return the wrong result!
You will then need to override these functions:
onCreate(): Initialize your provider. The Android system calls this method immediately after it creates your provider. Notice that your provider is not created until a ContentResolver object tries to access it.
getType(): Return the MIME type corresponding to a content URI
query(): Retrieve data from your provider. Use the arguments to select the table to query, the rows and columns to return, and the sort order of the result. Return the data as a Cursor object.
Insert a new row into your provider. Use the arguments to select the destination table and to get the column values to use. Return a content URI for the newly-inserted row.
delete(): Delete rows from your provider. Use the arguments to select the table and the rows to delete. Return the number of rows deleted.
update(): Update existing rows in your provider. Use the arguments to select the table and rows to update and to get the new column values. Return the number of rows updated.
4) Update manifest file