Android Tutorial

(avery) #1
Android Tutorial 351

First, the code gets an instance of a SQLiteQueryBuilder object,
which builds up a query with some method calls. Then, the
setTables() method configures which table in the database is used.
The UriMatcher class checks to see which specific rows are
requested. UriMatcher is discussed in greater detail later.

Next, the actual query is called. The content provider query has
fewer specifications than the SQLite query, so the parameters are
passed through and the rest is ignored. The instance of the SQLite
database is read-only. Because this is only a query for data, it’s
acceptable.

Finally, the Cursor needs to know if the source data has changed.
This is done by a call to the setNotificationUri() method telling it
which URI to watch for data changes. The call to the application’s
query() method might be called from multiple threads, as it calls to
update(), so it’s possible the data can change after the Cursor is
returned. Doing this keeps the data synchronized.

Exploring the UriMatcher Class

The UriMatcher class is a helper class for pattern matching on the
URIs that are passed to this content provider. It is used frequently
in the implementations of the content provider functions that must
be implemented. Here is the UriMatcher used in these sample
implementations:

public static final String AUTHORITY =
“com.androidbook.TrackPointProvider”
private static final int TRACKPOINTS = 1;
private static final int TRACKPOINT_ID = 10;
private static final UriMatcher sURIMatcher =

Free download pdf