Most visited

Recently visited

Added in API level 1

UriMatcher

public class UriMatcher
extends Object

java.lang.Object
   ↳ android.content.UriMatcher


工具类有助于匹配内容提供者中的URI。

要使用这个类,建立一个UriMatcher对象树。 例如:

    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);

    static
    {
        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);
    }

从API级别JELLY_BEAN_MR2开始,路径可以JELLY_BEAN_MR2斜杠开始。 例如:

        sURIMatcher.addURI("contacts", "/people", PEOPLE);

然后,当您需要与URI进行匹配时,请致电match(Uri) ,提供您已获得的URL。 您可以使用结果来构建查询,返回一个类型,插入或删除一行或任何您需要的内容,而无需重复所有您需要的if-else逻辑。 例如:

    public String getType(Uri url)
    {
        int match = sURIMatcher.match(url);
        switch (match)
        {
            case PEOPLE:
                return "vnd.android.cursor.dir/person";
            case PEOPLE_ID:
                return "vnd.android.cursor.item/person";
... snip ...
                return "vnd.android.cursor.dir/snail-mail";
            case PEOPLE_ADDRESS_ID:
                return "vnd.android.cursor.item/snail-mail";
            default:
                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 "vnd.android.cursor.dir/person";
                } else if (pathSegments.size() == 3) {
                    return "vnd.android.cursor.item/person";
... snip ...
                    return "vnd.android.cursor.dir/snail-mail";
                } else if (pathSegments.size() == 3) {
                    return "vnd.android.cursor.item/snail-mail";
                }
            }
        }
        return null;
    }

     
     
     

Summary

Constants

int NO_MATCH

Public constructors

UriMatcher(int code)

创建URI树的根节点。

Public methods

void addURI(String authority, String path, int code)

添加要匹配的URI,以及匹配此URI时返回的代码。

int match(Uri uri)

尝试匹配URL中的路径。

Inherited methods

From class java.lang.Object

Constants

NO_MATCH

Added in API level 1
int NO_MATCH

常量值:-1(0xffffffff)

Public constructors

UriMatcher

Added in API level 1
UriMatcher (int code)

创建URI树的根节点。

Parameters
code int: the code to match for the root URI

Public methods

addURI

Added in API level 1
void addURI (String authority, 
                String path, 
                int code)

添加要匹配的URI,以及匹配此URI时返回的代码。 URI节点可以是完全匹配字符串,与任何文本匹配的标记“*”或仅匹配数字的标记“#”。

从API级别 JELLY_BEAN_MR2 ,此方法将接受路径中的前导斜杠。

Parameters
authority String: the authority to match
path String: 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 int: the code that is returned when a URI is matched against the given components. Must be positive.

match

Added in API level 1
int match (Uri uri)

尝试匹配URL中的路径。

Parameters
uri Uri: The url whose path we will match against.
Returns
int The code for the matched node (added using addURI), or -1 if there is no matched node.

Hooray!