Using Android's UriMatcher

Posted on 22 Mar 2013 by Eric Oestrich

When setting up a ContentProvider, I didn’t really see any good explanation of how to use the UriMatcher class. Here is a small example of it in a ContentProvider.

public class MyProvider extends ContentProvider {
  private static final int ORDERS = 1;
  private static final int ORDERS_ID = 2;

  private static final int ITEMS = 3;
  private static final int ITEMS_ID = 4;

  private static final UriMatcher mUriMatcher = new UriMatcher(

  // The # sign will match numbers
  static {
    mUriMatcher.addURI(Constants.AUTHORITY, "orders", ORDERS);
    mUriMatcher.addURI(Constants.AUTHORITY, "orders/#", ORDERS_ID);

    mUriMatcher.addURI(Constants.AUTHORITY, "items", ITEMS);
    mUriMatcher.addURI(Constants.AUTHORITY, "items/#", ITEMS_ID);

  public int delete(Uri uri, String where, String[] whereArgs) {
    int count = 0;

    // Switch on the match, it will return the integer you passed in previously
    switch (mUriMatcher.match(uri)) {
    case ORDERS:
      count = database.getWritableDatabase().delete("orders", where, whereArgs);
      getContext().getContentResolver().notifyChange(uri, null);
    case ITEMS:
      count = database.getWritableDatabase().delete("items", where, whereArgs);
      getContext().getContentResolver().notifyChange(uri, null);

    return count;

  public Cursor query(Uri uri, String[] projection, String selection,
      String[] selectionArgs, String sortOrder) {

    SQLiteQueryBuilder qb = new SQLiteQueryBuilder();

    String defaultOrder = DatabaseHelper.DEFAULT_SORT_ORDER; // "id"

    switch (uriMatcher.match(uri)) {
    case ORDERS:
    case ORDERS_ID:
      defaultOrder = DatabaseHelper.DEFAULT_SORT_ORDER_ORDERS; // "date DESC"
    case ITEMS:
    case ITEMS_ID:

    String orderBy;

    if (TextUtils.isEmpty(sortOrder)) {
      orderBy = defaultOrder;
    } else {
      orderBy = sortOrder;

    Cursor cursor = qb.query(database.getReadableDatabase(), projection,
        selection, selectionArgs, null, null, orderBy);

    cursor.setNotificationUri(getContext().getContentResolver(), uri);

    return cursor;
comments powered by Disqus
Creative Commons License
This site's content is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License unless otherwise specified. Code on this site is licensed under the MIT License unless otherwise specified.