Login     Sign Up
Cyril Sermon (@admin)
9 months ago
40 Views

Exposing Access to the Data Source

You can expose queries and transactions with your Content Provider by implementing the delete, insert, update, and query methods.

These methods act as a generic interface to the underlying data source, allowing Android applications to share data across application boundaries without having to publish separate interfaces for each applica-tion. 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).

The following code snippet shows the skeleton code for implementing queries and transactions for a Content Provider. Notice that the UriMatcher object is used to refine the transaction and query requests.

@Override

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 pro-vider 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 the forms:

❑Single Item vnd.


<companyname>.cursor.item/<contenttype>

❑All Items vnd.


<companyName>.cursor.dir/<contenttype>

The following code snippet shows how to override the getType method to return the correct MIME type based on the URI passed in:

@Override

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);

}

}

Registering Your Provider

Once you have completed your Content Provider, it must be added to the application manifest.

Use the authorities tag to specify its address, as shown in the following XML snippet:

<provider android:name=”MyProvider” android:authorities=”com.paad.provider.myapp”/>