Resolving ContentProvider URLs
An Android ContentProvider
uses URLs to identify data. Typically, you can use a URL to identify a specific piece of data, such as a single reminder, or all reminders in your database. If you store other types of data there, you can use URLs for them, too.
In your application, you use two kinds of URLs — content://com.dummies.android.taskreminder.ReminderProvider/reminder
to retrieve a list of all reminders in your database or content://com.dummies.android.taskreminder.ReminderProvider/reminder/9
to retrieve a specific reminder from the database (in this case the reminder with the ID of 9).
These content provider URLs are undoubtedly similar to the URLs you’re already familiar with. Their main differences are described in this list:
content://: A ContentProvider
begins with content://
rather than with http://
.
com.dummies.android.taskreminder.ReminderProvider: The second part of the URL identifies the authority (the ReminderProvider
ContentProvider
) of the content. Though this string can be virtually anything, convention dictates using the fully qualified name of your ContentProvider
.
reminder: The third part of the URL identifies the path — in this case, the type of data you’re looking up. This string identifies which table in the database to read. If the application stores multiple types in the database (say, a list of users in addition to a list of reminders), a second type of path might be named user
.
9: In the first URL, the path ends with reminder
. However, in the second URL, the path continues to include the specific ID of the reminder being requested.
Before you can use the ContentProvider
, ensure that it’s listed in the Android Manifest.xml
file, by adding this code before the </application>
tag:
<provider
android:name=”com.dummies.android.taskreminder.ReminderProvider”
android:authorities=”com.dummies.android.taskreminder.ReminderProvider”
android:exported=”false”
/>
It tells Android that a ContentProvider
named ReminderProvider
will handle URLs that use the specific authority of com.dummies.android.taskreminder.ReminderProvider
. It also indicates that the data in the provider is not exported to other apps on the user’s phone. In general, you should set exported=”false”
unless you want to make your provider available to other apps.
Now you have to add the code to support these URLs in your Content Provider
. Open ReminderProvider
and add the following lines to the class:
// Content Provider Uri and Authority
public static String AUTHORITY = “com.dummies.android.taskreminder.ReminderProvider”; →2
public static final Uri CONTENT_URI = Uri.parse(“content://” + AUTHORITY
+ “/reminder”); →4
// MIME types used for searching words or looking up a single definition
public static final String REMINDERS_MIME_TYPE = ContentResolver.CURSOR_DIR_BASE_TYPE
+ “/vnd.com.dummies.android.taskreminder.reminder”; →8
public static final String REMINDER_MIME_TYPE = ContentResolver.CURSOR_ITEM_BASE_TYPE
+ “/vnd.com.dummies.android.taskreminder.reminder”;
// UriMatcher stuff
private static final int LIST_REMINDER = 0; →13
private static final int ITEM_REMINDER = 1;
private static final UriMatcher sURIMatcher = buildUriMatcher(); →15
/**
* Builds up a UriMatcher for search suggestion and shortcut refresh
* queries.
*/
private static UriMatcher buildUriMatcher() {
UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH); →22
matcher.addURI(AUTHORITY, “reminder”, LIST_REMINDER); →23
matcher.addURI(AUTHORITY, “reminder/#”, ITEM_REMINDER); →24
return matcher;
}
/**
* This method is required in order to query the supported types. It’s also
* useful in the query() method to determine the type of Uri received.
*/
@Override
public String getType(Uri uri) { →33
switch (sURIMatcher.match(uri)) {
case LIST_REMINDER:
return REMINDERS_MIME_TYPE;
case ITEM_REMINDER:
return REMINDER_MIME_TYPE;
default:
throw new IllegalArgumentException(“Unknown Uri: “ + uri);
}
}
This chunk of code may seem intimidating, but it consists mostly of constants with one useful method (getType()
). Here’s how the numbered lines work:
→2 The authority for the ContentProvider
— by convention, the same as the fully qualified class name. This value must match the value you added to the AndroidManifest.xml
file for the provider authorities.
→4 The base URL (or URI) for the ContentProvider
. Every time your application asks for data for this URL, Android routes the request to this ContentProvider
.
The ContentProvider
supports two types of URLs: one for listing all reminders and one for listing a specific reminder.
The first type of URL is the CONTENT_URI
, and the second one is the CONTENT_URI
with the reminder ID appended to the end.
→8 Because the ContentProvider
supports two types of data, it defines two types (or MIME types) for this data. MIME types are simply strings commonly used on the web to identify data types. For example, web HTML content typically has a MIME type of text/html
, and audio MP3 files have audio/mpeg3
. Because the reminders are of no known standard type, you can make up MIME type strings as long as you follow Android and MIME conventions.
The List MIME type begins with ContentResolver.CURSOR_DIR_BASE_TYPE
, and the individual Reminder MIME type begins with ContentResolver.CURSOR_ITEM_BASE_TYPE
. DIR
represents the list, and ITEM
represents the item — simple enough.
This line also checks to see whether the subtype (which follows the /
) begins with vnd
. The subtype is followed by the fully qualified class name and the type of data — in this case, com.dummies. android.taskreminder
and reminder
. Visit http://developer.android.com/reference/android/content/ContentResolver.html
for more information about the Android conventions for MIME types.
→13 Uses another constant to identify list types versus item types, which are ints
.
→15 The UriMatcher
is used to determine the URL type: list or item. You build a UriMatcher
using the method named build UriMatcher()
on line 21.
→22 Creates the UriMatcher
, which can indicate whether a given URL is the list type or item type. The UriMatcher.NO_MATCH
parameter tells the application which default value to return for a match.
→23 Defines the list type. Any URL that uses the com.dummies.android.taskreminder.ReminderProvider
authority and has a path named “reminder”
returns the value LIST_REMINDER
.
→24 Defines the item type. Any URL that uses the com.dummies.android.taskreminder.ReminderProvider
authority and has a path that looks like reminder/#
(where #
is a number) returns the value ITEM_REMINDER
.
→33 Uses the UriMatcher
on line 15 to determine which MIME type to return. If the URL is a list URL, it returns REMINDERS_MIME_TYPE
. If it’s an item URL, it returns REMINDER_MIME_TYPE
.