java android.content.UriMatcher 的含义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26901644/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 10:50:27  来源:igfitidea点击:

Meaning of android.content.UriMatcher

javaandroiduri

提问by Malwinder Singh

What is Uri Matcher in android.content.UriMatcher

什么是 Uri Matcher android.content.UriMatcher

How to use it? Can someone please explain meaning of following three line of code?

如何使用它?有人可以解释以下三行代码的含义吗?

  uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
  uriMatcher.addURI(PROVIDER_NAME, "cte", uriCode);
  uriMatcher.addURI(PROVIDER_NAME, "cte/*", uriCode);
  int res = uriMatcher.match(uri);

回答by Bruce

UriMatcher is a handy class when you are writing a ContentProvider or some other class that needs to respond to a number of different URIs. In your example, a user could query your provider with URIs such as:

当您编写 ContentProvider 或其他需要响应多个不同 URI 的类时,UriMatcher 是一个方便的类。在您的示例中,用户可以使用 URI 查询您的提供商,例如:

myprovider://cte

or

或者

myprovider://cte/somestring

When you construct a UriMatcher, you need to have separate codes for each URI (not just "uriCode" as in your example). I usually make my UriMatcher instance static, and add the URIs in a static constructor:

当您构造 UriMatcher 时,您需要为每个 URI 使用单独的代码(不仅仅是示例中的“uriCode”)。我通常将 UriMatcher 实例设为静态,并在静态构造函数中添加 URI:

private static final int CTE_ALL = 1;
private static final int CTE_FIND = 2;
private static final UriMatcher uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
static {
    uriMatcher.addURI(PROVIDER_NAME, "cte", CTE_ALL);
    uriMatcher.addURI(PROVIDER_NAME, "cte/*", CTE_FIND);
}

Then in your ContentProvider you would do something like this in your query method:

然后在您的 ContentProvider 中,您将在查询方法中执行以下操作:

Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
    int res = uriMatcher.match(uri);
    switch (res) {
        case CTE_ALL:
            //TODO create a results Cursor with all the CTE results
            break;
        case CTE_FIND:
            //TODO create a results Cursor with the single CTE requested
            break;
    }
    return results;
}

回答by adityah

I found the following videos to be useful:

我发现以下视频很有用:

URI Basics

URI 基础知识

URI Matcher

URI匹配器

In essence, what you are trying to do is, have an ID or a number associated to different URIs. When you use addUri, a code/number/ID gets created against the URI. When you request a match(), the corresponding code is returned.

从本质上讲,您要做的是将一个 ID 或一个数字关联到不同的 URI。当您使用 addUri 时,会根据 URI 创建一个代码/数字/ID。当您请求 match() 时,会返回相应的代码。

回答by Aleksey Yaremenko

One more thing I wanted to add that wasn't clear for me first time I used UriMatcher.

我想补充的另一件事是我第一次使用 UriMatcher 时不清楚。

Is that if you want to parse an HTTP url then as an AUTHORITYparameter in the addURIyou need to pass the target domain name. For example:

如果你想解析一个 HTTP url,那么作为addURI 中AUTHORITY参数,你需要传递目标域名。例如:

Uri mUri = Uri.parse("http://example.com/foo");
UriMatcher sURIMatcher = new UriMatcher(UriMatcher.NO_MATCH);

// if not "example.com" below the match will always return -1 result
sURIMatcher.addURI("example.com", "/foo", 123);

int match = sURIMatcher.match(mUri);

UriMatcher Documentationdoesn't cover this case and it's not clear what's this authority parameter for. Gosh if I knew that it would save me some time!

UriMatcher 文档没有涵盖这种情况,也不清楚这个权限参数的用途是什么。天哪,如果我知道这会为我节省一些时间!