Compartilhar via


ContactsContract.RawContacts Class

Definition

Constants for the raw contacts table, which contains one row of contact information for each person in each synced account.

[Android.Runtime.Register("android/provider/ContactsContract$RawContacts", DoNotGenerateAcw=true)]
public sealed class ContactsContract.RawContacts : Java.Lang.Object
[<Android.Runtime.Register("android/provider/ContactsContract$RawContacts", DoNotGenerateAcw=true)>]
type ContactsContract.RawContacts = class
    inherit Object
Inheritance
ContactsContract.RawContacts
Attributes

Remarks

Constants for the raw contacts table, which contains one row of contact information for each person in each synced account. Sync adapters and contact management apps are the primary consumers of this API.

<h3>Aggregation</h3>

As soon as a raw contact is inserted or whenever its constituent data changes, the provider will check if the raw contact matches other existing raw contacts and if so will aggregate it with those. The aggregation is reflected in the RawContacts table by the change of the #CONTACT_ID field, which is the reference to the aggregate contact.

Changes to the structured name, organization, phone number, email address, or nickname trigger a re-aggregation.

See also AggregationExceptions for a mechanism to control aggregation programmatically.

<h3>Operations</h3> <dl> <dt><b>Insert</b></dt> <dd>

Raw contacts can be inserted incrementally or in a batch. The incremental method is more traditional but less efficient. It should be used only if no Data values are available at the time the raw contact is created:

ContentValues values = new ContentValues();
            values.put(RawContacts.ACCOUNT_TYPE, accountType);
            values.put(RawContacts.ACCOUNT_NAME, accountName);
            Uri rawContactUri = getContentResolver().insert(RawContacts.CONTENT_URI, values);
            long rawContactId = ContentUris.parseId(rawContactUri);

</p>

Once Data values become available, insert those. For example, here's how you would insert a name:

values.clear();
            values.put(Data.RAW_CONTACT_ID, rawContactId);
            values.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);
            values.put(StructuredName.DISPLAY_NAME, &quot;Mike Sullivan&quot;);
            getContentResolver().insert(Data.CONTENT_URI, values);

</p>

The batch method is by far preferred. It inserts the raw contact and its constituent data rows in a single database transaction and causes at most one aggregation pass.

ArrayList&lt;ContentProviderOperation&gt; ops =
                     new ArrayList&lt;ContentProviderOperation&gt;();
            ...
            int rawContactInsertIndex = ops.size();
            ops.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI)
                     .withValue(RawContacts.ACCOUNT_TYPE, accountType)
                     .withValue(RawContacts.ACCOUNT_NAME, accountName)
                     .build());

            ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
                     .withValueBackReference(Data.RAW_CONTACT_ID, rawContactInsertIndex)
                     .withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE)
                     .withValue(StructuredName.DISPLAY_NAME, &quot;Mike Sullivan&quot;)
                     .build());

            getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);

</p>

Note the use of ContentProviderOperation.Builder#withValueBackReference(String, int) to refer to the as-yet-unknown index value of the raw contact inserted in the first operation.

<dt><b>Update</b></dt> <dd>

Raw contacts can be updated incrementally or in a batch. Batch mode should be used whenever possible. The procedures and considerations are analogous to those documented above for inserts.

</dd> <dt><b>Delete</b></dt> <dd>

When a raw contact is deleted, all of its Data rows as well as StatusUpdates, AggregationExceptions, PhoneLookup rows are deleted automatically. When all raw contacts associated with a Contacts row are deleted, the Contacts row itself is also deleted automatically.

The invocation of resolver.delete(...), does not immediately delete a raw contacts row. Instead, it sets the #DELETED flag on the raw contact and removes the raw contact from its aggregate contact. The sync adapter then deletes the raw contact from the server and finalizes phone-side deletion by calling resolver.delete(...) again and passing the ContactsContract#CALLER_IS_SYNCADAPTER query parameter.

Some sync adapters are read-only, meaning that they only sync server-side changes to the phone, but not the reverse. If one of those raw contacts is marked for deletion, it will remain on the phone. However it will be effectively invisible, because it will not be part of any aggregate contact. </dd>

<dt><b>Query</b></dt> <dd>

It is easy to find all raw contacts in a Contact:

Cursor c = getContentResolver().query(RawContacts.CONTENT_URI,
                     new String[]{RawContacts._ID},
                     RawContacts.CONTACT_ID + "=?",
                     new String[]{String.valueOf(contactId)}, null);

</p>

To find raw contacts within a specific account, you can either put the account name and type in the selection or pass them as query parameters. The latter approach is preferable, especially when you can reuse the URI:

Uri rawContactUri = RawContacts.CONTENT_URI.buildUpon()
                     .appendQueryParameter(RawContacts.ACCOUNT_NAME, accountName)
                     .appendQueryParameter(RawContacts.ACCOUNT_TYPE, accountType)
                     .build();
            Cursor c1 = getContentResolver().query(rawContactUri,
                     RawContacts.STARRED + "&lt;&gt;0", null, null, null);
            ...
            Cursor c2 = getContentResolver().query(rawContactUri,
                     RawContacts.DELETED + "&lt;&gt;0", null, null, null);

</p>

The best way to read a raw contact along with all the data associated with it is by using the Entity directory. If the raw contact has data rows, the Entity cursor will contain a row for each data row. If the raw contact has no data rows, the cursor will still contain one row with the raw contact-level information.

Uri rawContactUri = ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactId);
            Uri entityUri = Uri.withAppendedPath(rawContactUri, Entity.CONTENT_DIRECTORY);
            Cursor c = getContentResolver().query(entityUri,
                     new String[]{RawContacts.SOURCE_ID, Entity.DATA_ID, Entity.MIMETYPE, Entity.DATA1},
                     null, null, null);
            try {
                while (c.moveToNext()) {
                    String sourceId = c.getString(0);
                    if (!c.isNull(1)) {
                        String mimeType = c.getString(2);
                        String data = c.getString(3);
                        ...
                    }
                }
            } finally {
                c.close();
            }

</p> </dd> </dl> <h2>Columns</h2>

<table class="jd-sumtable"> <tr> <th colspan='4'>RawContacts</th> </tr> <tr> <td>long</td> <td>#_ID</td> <td>read-only</td> <td>Row ID. Sync adapters should try to preserve row IDs during updates. In other words, it is much better for a sync adapter to update a raw contact rather than to delete and re-insert it.</td> </tr> <tr> <td>long</td> <td>#CONTACT_ID</td> <td>read-only</td> <td>The ID of the row in the ContactsContract.Contacts table that this raw contact belongs to. Raw contacts are linked to contacts by the aggregation process, which can be controlled by the #AGGREGATION_MODE field and AggregationExceptions.</td> </tr> <tr> <td>int</td> <td>#AGGREGATION_MODE</td> <td>read/write</td> <td>A mechanism that allows programmatic control of the aggregation process. The allowed values are #AGGREGATION_MODE_DEFAULT, #AGGREGATION_MODE_DISABLED and #AGGREGATION_MODE_SUSPENDED. See also AggregationExceptions.</td> </tr> <tr> <td>int</td> <td>#DELETED</td> <td>read/write</td> <td>The "deleted" flag: "0" by default, "1" if the row has been marked for deletion. When android.content.ContentResolver#delete is called on a raw contact, it is marked for deletion and removed from its aggregate contact. The sync adaptor deletes the raw contact on the server and then calls ContactResolver.delete once more, this time passing the ContactsContract#CALLER_IS_SYNCADAPTER query parameter to finalize the data removal.</td> </tr> <tr> <td>int</td> <td>#STARRED</td> <td>read/write</td> <td>An indicator for favorite contacts: '1' if favorite, '0' otherwise. Changing this field immediately affects the corresponding aggregate contact: if any raw contacts in that aggregate contact are starred, then the contact itself is marked as starred.</td> </tr> <tr> <td>String</td> <td>#CUSTOM_RINGTONE</td> <td>read/write</td> <td>A custom ringtone associated with a raw contact. Typically this is the URI returned by an activity launched with the android.media.RingtoneManager#ACTION_RINGTONE_PICKER intent. To have an effect on the corresponding value of the aggregate contact, this field should be set at the time the raw contact is inserted. To set a custom ringtone on a contact, use the field ContactsContract.Contacts#CUSTOM_RINGTONE Contacts.CUSTOM_RINGTONE instead.</td> </tr> <tr> <td>int</td> <td>#SEND_TO_VOICEMAIL</td> <td>read/write</td> <td>An indicator of whether calls from this raw contact should be forwarded directly to voice mail ('1') or not ('0'). To have an effect on the corresponding value of the aggregate contact, this field should be set at the time the raw contact is inserted.</td> </tr> <tr> <td>String</td> <td>#ACCOUNT_NAME</td> <td>read/write-once</td> <td>The name of the account instance to which this row belongs, which when paired with #ACCOUNT_TYPE identifies a specific account. For example, this will be the Gmail address if it is a Google account. It should be set at the time the raw contact is inserted and never changed afterwards.</td> </tr> <tr> <td>String</td> <td>#ACCOUNT_TYPE</td> <td>read/write-once</td> <td>

The type of account to which this row belongs, which when paired with #ACCOUNT_NAME identifies a specific account. It should be set at the time the raw contact is inserted and never changed afterwards.

To ensure uniqueness, new account types should be chosen according to the Java package naming convention. Thus a Google account is of type "com.google".

</td> </tr> <tr> <td>String</td> <td>#DATA_SET</td> <td>read/write-once</td> <td>

The data set within the account that this row belongs to. This allows multiple sync adapters for the same account type to distinguish between each others' data. The combination of #ACCOUNT_TYPE, #ACCOUNT_NAME, and #DATA_SET identifies a set of data that is associated with a single sync adapter.

This is empty by default, and is completely optional. It only needs to be populated if multiple sync adapters are entering distinct data for the same account type and account name.

It should be set at the time the raw contact is inserted and never changed afterwards.

</td> </tr> <tr> <td>String</td> <td>#SOURCE_ID</td> <td>read/write</td> <td>String that uniquely identifies this row to its source account. Typically it is set at the time the raw contact is inserted and never changed afterwards. The one notable exception is a new raw contact: it will have an account name and type (and possibly a data set), but no source id. This indicates to the sync adapter that a new contact needs to be created server-side and its ID stored in the corresponding SOURCE_ID field on the phone. </td> </tr> <tr> <td>int</td> <td>#VERSION</td> <td>read-only</td> <td>Version number that is updated whenever this row or its related data changes. This field can be used for optimistic locking of a raw contact. </td> </tr> <tr> <td>int</td> <td>#DIRTY</td> <td>read/write</td> <td>Flag indicating that #VERSION has changed, and this row needs to be synchronized by its owning account. The value is set to "1" automatically whenever the raw contact changes, unless the URI has the ContactsContract#CALLER_IS_SYNCADAPTER query parameter specified. The sync adapter should always supply this query parameter to prevent unnecessary synchronization: user changes some data on the server, the sync adapter updates the contact on the phone (without the CALLER_IS_SYNCADAPTER flag) flag, which sets the DIRTY flag, which triggers a sync to bring the changes to the server. </td> </tr> <tr> <td>String</td> <td>#SYNC1</td> <td>read/write</td> <td>Generic column provided for arbitrary use by sync adapters. The content provider stores this information on behalf of the sync adapter but does not interpret it in any way. </td> </tr> <tr> <td>String</td> <td>#SYNC2</td> <td>read/write</td> <td>Generic column for use by sync adapters. </td> </tr> <tr> <td>String</td> <td>#SYNC3</td> <td>read/write</td> <td>Generic column for use by sync adapters. </td> </tr> <tr> <td>String</td> <td>#SYNC4</td> <td>read/write</td> <td>Generic column for use by sync adapters. </td> </tr> </table>

Java documentation for android.provider.ContactsContract.RawContacts.

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.

Fields

ContentItemType

The MIME type of the results when a raw contact ID is appended to #CONTENT_URI, yielding a subdirectory of a single person.

ContentType

The MIME type of the results from #CONTENT_URI when a specific ID value is not provided, and multiple raw contacts may be returned.

Properties

Class

Returns the runtime class of this Object.

(Inherited from Object)
ContentUri

The content:// style URI for this table, which requests a directory of raw contact rows matching the selection criteria.

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.

(Inherited from Object)
ThresholdType

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

(Inherited from Object)

Methods

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)
GetContactLookupUri(ContentResolver, Uri)

Build a android.provider.ContactsContract.Contacts#CONTENT_LOOKUP_URI style Uri for the parent android.provider.ContactsContract.Contacts entry of the given RawContacts entry.

GetHashCode()

Returns a hash code value for the object.

(Inherited from Object)
GetLocalAccountName(Context)

The default value used for #ACCOUNT_NAME of raw contacts when they are inserted without a value for this column.

GetLocalAccountType(Context)

The default value used for #ACCOUNT_TYPE of raw contacts when they are inserted without a value for this column.

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