Fulfilling MIMEtype Contracts

The BookProvider content provider must also implement the getType() method to return a MIME type for a given URI. This method, like many other methods of a content provider, is overloaded with respect to the incoming URI. As a result, the first responsibility of the getType() method is to distinguish the type of the URI. Is it a collection of books, or a single book?

As we pointed out in the previous section, we will use the UriMatcher to decipher this URI type. Depending on this URI, the BookTableMetaData class has defined the MIME-type constants to return for each URI. Without further ado, we present the complete code for the getType() method implementation in Listing 3-26.

Listing 3-26. The getTypeO Method Implementation ^Override public String getType(Uri uri) {

switch (sUriMatcher.match(uri)) { case INCOMING_BOOK_COLLECTION_URI_INDICATOR: return BookTableMetaData.CONTENT_TYPE;


return BookTableMetaData.CONTENT_ITEM_TYPE;


throw new IllegalArgumentException("Unknown URI " + uri);

0 0

Post a comment