Exposing Access to the Data Source

Expose queries and transactions on your Content Provider by implementing the delete, insert, update, and query methods.

These methods are the interface used by the Content Resolver to access the underlying data. They allow applications to share data across application boundaries without having to publish different interfaces for each data source.

The most common scenario is to use a Content Provider to expose a private SQLite database, but within these methods you can access any source of data (including files or application instance variables).

Listing 7-11 shows the skeleton code for implementing queries and transactions within a Content Provider. Notice that the UriMatcher object is used to refine the transaction and query requests.

LISTING 7-11: Implementing queries and transactions within a Content Provider Available for download on ©Override

Wrax.com public Cursor query(Uri uri,

String[] projection, String selection, String[] selectionArgs, String sort) {

// If this is a row query, limit the result set to the passed in row. switch (uriMatcher.match(uri)) { case SINGLE_ROW :

// TODO: Modify selection based on row id, where: // rowNumber = uri.getPathSegments().get(1));

return null;

©Override public Uri insert(Uri _uri, ContentValues _initialValues) { long rowID = [ ... Add a new item ... ]

// Return a URI to the newly added item, if (rowID >0) {

return ContentUris.withAppendedId(CONTENT_URI, rowID);

throw new SQLException("Failed to add new item into " + _uri);

©Override public int delete(Uri uri, String where, String[] whereArgs) { switch (uriMatcher.match(uri)) { case ALLROWS: case SINGLE_ROW:

default: throw new IllegalArgumentException("Unsupported URI:" + uri);

©Override public int update(Uri uri, ContentValues values, String where, String[] whereArgs) {

switch (uriMatcher.match(uri)) { case ALLROWS: case SINGLE_ROW:

default: throw new IllegalArgumentException("Unsupported URI:" + uri);

The final step in creating a Content Provider is defining the MIME type that identifies the data the provider returns.

Override the getType method to return a String that uniquely describes your data type. The type returned should include two forms, one for a single entry and another for all the entries, following these forms:

V Single item vnd.<companyname>.cursor.item/<contenttype>

V All items vnd.<companyName>.cursor.dir/<contenttype>

Listing 7-12 shows how to override the getType method to return the correct MIME type based on the URI passed in.

LISTING 7-12: Returning a Content Provider MIME type Available for download on ©Override

Wrox.com public String getType(Uri _uri) {

switch (uriMatcher.match(_uri)) {

case ALLROWS: return "vnd.paad.cursor.dir/myprovidercontent"; case SINGLE_ROW: return "vnd.paad.cursor.item/myprovidercontent"; default: throw new IllegalArgumentException("Unsupported URI: " + _uri);

Mobile Apps Made Easy

Mobile Apps Made Easy

Quick start guide to skyrocket your offline and online business success with mobile apps. If you know anything about mobile devices, you’ve probably heard that famous phrase coined by one of the mobile device’s most prolific creators proclaiming that there’s an app for pretty much everything.

Get My Free Training Guide


Post a comment