What is Uri Matcher in 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);
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:
or
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:
Then in your ContentProvider you would do something like this in your query method:
I found the following videos to be useful:
URI Basics
URI Matcher
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.