IT. Expert System.

Android Reference



Class UriMatcher

  • public class UriMatcher
    extends Object
    Utility class to aid in matching URIs in content providers.

    To use this class, build up a tree of UriMatcher objects. For example:

        private static final int PEOPLE = 1;
        private static final int PEOPLE_ID = 2;
        private static final int PEOPLE_PHONES = 3;
        private static final int PEOPLE_PHONES_ID = 4;
        private static final int PEOPLE_CONTACTMETHODS = 7;
        private static final int PEOPLE_CONTACTMETHODS_ID = 8;
        private static final int DELETED_PEOPLE = 20;
        private static final int PHONES = 9;
        private static final int PHONES_ID = 10;
        private static final int PHONES_FILTER = 14;
        private static final int CONTACTMETHODS = 18;
        private static final int CONTACTMETHODS_ID = 19;
        private static final int CALLS = 11;
        private static final int CALLS_ID = 12;
        private static final int CALLS_FILTER = 15;
        private static final UriMatcher sURIMatcher = new UriMatcher(UriMatcher.NO_MATCH);
            sURIMatcher.addURI("contacts", "people", PEOPLE);
            sURIMatcher.addURI("contacts", "people/#", PEOPLE_ID);
            sURIMatcher.addURI("contacts", "people/#/phones", PEOPLE_PHONES);
            sURIMatcher.addURI("contacts", "people/#/phones/#", PEOPLE_PHONES_ID);
            sURIMatcher.addURI("contacts", "people/#/contact_methods", PEOPLE_CONTACTMETHODS);
            sURIMatcher.addURI("contacts", "people/#/contact_methods/#", PEOPLE_CONTACTMETHODS_ID);
            sURIMatcher.addURI("contacts", "deleted_people", DELETED_PEOPLE);
            sURIMatcher.addURI("contacts", "phones", PHONES);
            sURIMatcher.addURI("contacts", "phones/filter/*", PHONES_FILTER);
            sURIMatcher.addURI("contacts", "phones/#", PHONES_ID);
            sURIMatcher.addURI("contacts", "contact_methods", CONTACTMETHODS);
            sURIMatcher.addURI("contacts", "contact_methods/#", CONTACTMETHODS_ID);
            sURIMatcher.addURI("call_log", "calls", CALLS);
            sURIMatcher.addURI("call_log", "calls/filter/*", CALLS_FILTER);
            sURIMatcher.addURI("call_log", "calls/#", CALLS_ID);

    Then when you need to match against a URI, call match(, providing the URL that you have been given. You can use the result to build a query, return a type, insert or delete a row, or whatever you need, without duplicating all of the if-else logic that you would otherwise need. For example:

        public String getType(Uri url)
            int match = sURIMatcher.match(url);
            switch (match)
                case PEOPLE:
                    return "";
                case PEOPLE_ID:
                    return "";
    ... snip ...
                    return "";
                case PEOPLE_ADDRESS_ID:
                    return "";
                    return null;
    instead of:
        public String getType(Uri url)
            List pathSegments = url.getPathSegments();
            if (pathSegments.size() >= 2) {
                if ("people".equals(pathSegments.get(1))) {
                    if (pathSegments.size() == 2) {
                        return "";
                    } else if (pathSegments.size() == 3) {
                        return "";
    ... snip ...
                        return "";
                    } else if (pathSegments.size() == 3) {
                        return "";
            return null;
    • Constructor Detail

      • UriMatcher

        public UriMatcher(int code)
        Creates the root node of the URI tree.
        code - the code to match for the root URI
    • Method Detail

      • addURI

        public void addURI(String authority,
                  String path,
                  int code)
        Add a URI to match, and the code to return when this URI is matched. URI nodes may be exact match string, the token "*" that matches any text, or the token "#" that matches only numbers.
        authority - the authority to match
        path - the path to match. * may be used as a wild card for any text, and # may be used as a wild card for numbers.
        code - the code that is returned when a URI is matched against the given components. Must be positive.
      • match

        public int match(Uri uri)
        Try to match against the path in a url.
        uri - The url whose path we will match against.
        The code for the matched node (added using addURI), or -1 if there is no matched node.


Android Reference

Java basics

Java Enterprise Edition (EE)

Java Standard Edition (SE)





Java Script








Design patterns

RFC (standard status)

RFC (proposed standard status)

RFC (draft standard status)

RFC (informational status)

RFC (experimental status)

RFC (best current practice status)

RFC (historic status)

RFC (unknown status)

IT dictionary

All information of this service is derived from the free sources and is provided solely in the form of quotations. This service provides information and interfaces solely for the familiarization (not ownership) and under the "as is" condition.
Copyright 2016 © ELTASK.COM. All rights reserved.
Site is optimized for mobile devices.
Downloads: 1054 / 159176162. Delta: 0.03357 с