共用方式為


UriMatcher Class

Definition

Utility class to aid in matching URIs in content providers.

[Android.Runtime.Register("android/content/UriMatcher", DoNotGenerateAcw=true)]
public class UriMatcher : Java.Lang.Object
[<Android.Runtime.Register("android/content/UriMatcher", DoNotGenerateAcw=true)>]
type UriMatcher = class
    inherit Object
Inheritance
UriMatcher
Attributes

Remarks

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

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

Starting from API level android.os.Build.VERSION_CODES#JELLY_BEAN_MR2, paths can start with a leading slash. For example:

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

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 "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<String> 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;
                }

Java documentation for android.content.UriMatcher.

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Constructors

UriMatcher(Int32)

Creates the root node of the URI tree.

UriMatcher(IntPtr, JniHandleOwnership)

A constructor used when creating managed representations of JNI objects; called by the runtime.

Fields

NoMatch

Properties

Class

Returns the runtime class of this Object.

(Inherited from Object)
Handle

The handle to the underlying Android instance.

(Inherited from Object)
JniIdentityHashCode (Inherited from Object)
JniPeerMembers
PeerReference (Inherited from Object)
ThresholdClass

This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.

ThresholdType

This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.

Methods

AddURI(String, String, Int32)

Add a URI to match, and the code to return when this URI is matched.

Clone()

Creates and returns a copy of this object.

(Inherited from Object)
Dispose() (Inherited from Object)
Dispose(Boolean) (Inherited from Object)
Equals(Object)

Indicates whether some other object is "equal to" this one.

(Inherited from Object)
GetHashCode()

Returns a hash code value for the object.

(Inherited from Object)
JavaFinalize()

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.

(Inherited from Object)
Match(Uri)

Try to match against the path in a url.

Notify()

Wakes up a single thread that is waiting on this object's monitor.

(Inherited from Object)
NotifyAll()

Wakes up all threads that are waiting on this object's monitor.

(Inherited from Object)
SetHandle(IntPtr, JniHandleOwnership)

Sets the Handle property.

(Inherited from Object)
ToArray<T>() (Inherited from Object)
ToString()

Returns a string representation of the object.

(Inherited from Object)
UnregisterFromRuntime() (Inherited from Object)
Wait()

Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>.

(Inherited from Object)
Wait(Int64, Int32)

Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>, or until a certain amount of real time has elapsed.

(Inherited from Object)
Wait(Int64)

Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>, or until a certain amount of real time has elapsed.

(Inherited from Object)

Explicit Interface Implementations

IJavaPeerable.Disposed() (Inherited from Object)
IJavaPeerable.DisposeUnlessReferenced() (Inherited from Object)
IJavaPeerable.Finalized() (Inherited from Object)
IJavaPeerable.JniManagedPeerState (Inherited from Object)
IJavaPeerable.SetJniIdentityHashCode(Int32) (Inherited from Object)
IJavaPeerable.SetJniManagedPeerState(JniManagedPeerStates) (Inherited from Object)
IJavaPeerable.SetPeerReference(JniObjectReference) (Inherited from Object)

Extension Methods

JavaCast<TResult>(IJavaObject)

Performs an Android runtime-checked type conversion.

JavaCast<TResult>(IJavaObject)
GetJniTypeName(IJavaPeerable)

Applies to