Most visited

Recently visited

Added in API level 5

ContactsContract.RawContacts

public static final class ContactsContract.RawContacts
extends Object implements BaseColumns, ContactsContract.RawContactsColumns, ContactsContract.ContactOptionsColumns, ContactsContract.ContactNameColumns, ContactsContract.SyncColumns

java.lang.Object
   ↳ android.provider.ContactsContract.RawContacts


原始联系人表的常量,其中包含每个已同步帐户中每个人的一行联系信息。 同步适配器和联系人管理应用程序是此API的主要使用者。

Aggregation

只要插入一个原始联系人或者其组成数据发生变化,提供商就会检查原始联系人是否与其他现有原始联系人匹配,如果是,则会将其与其他联系人进行汇总。 该聚合反映在ContactsContract.RawContacts表中,由CONTACT_ID字段的更改引起,该字段是对聚合联系人的引用。

对结构化名称,组织,电话号码,电子邮件地址或昵称的更改会触发重新聚合。

另请参阅 ContactsContract.AggregationExceptions了解以编程方式控制聚合的机制。

Operations

Insert

原始联系人可以增量或批量插入。 增量方法更传统但效率更低。 只有在创建原始联系人时没有ContactsContract.RawContacts.Data值可用的情况下才应使用它:

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

一旦ContactsContract.RawContacts.Data值可用,请插入这些值。 例如,以下是如何插入名称的方法:

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

批量方法是迄今为止优选的。 它将原始联系人及其组成数据行插入单个数据库事务中,并最多导致一次聚合传递。

 ArrayList<ContentProviderOperation> ops =
          new ArrayList<ContentProviderOperation>();
 ...
 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, "Mike Sullivan")
          .build());

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

请注意,使用 withValueBackReference(String, int)来引用在第一个操作中插入的原始联系人的尚未知的索引值。

Update

原始联系人可以逐步更新或批量更新。 应尽可能使用批处理模式。 程序和考虑类似于上面记载的插入程序。

Delete

当原始联系人被删除时,其所有Data行以及StatusUpdates,AggregationExceptions,PhoneLookup行都会自动删除。 当与Contacts行关联的所有原始联系人都被删除时, Contacts行本身也会自动删除。

调用resolver.delete(...) ,不会立即删除原始联系人行。 相反,它会在原始联系人上设置DELETED标志,并将原始联系人DELETED联系人中删除。 同步适配器然后从服务器删除原始联系人并通过再次调用resolver.delete(...)并传递CALLER_IS_SYNCADAPTER查询参数来完成电话端删除。

一些同步适配器是只读的,这意味着它们只将服务器端的更改同步到手机,但不会相反。 如果其中一个原始联系人被标记为删除,它将保留在电话上。 但是它会被有效地隐藏,因为它不会成为任何聚合联系人的一部分。

Query

很容易找到联系人中的所有原始联系人:

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

要在特定帐户中查找原始联系人,您可以将帐户名称和类型放入选择中,或将其作为查询参数传递。 后一种方法是可取的,尤其是当您可以重用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 + "<>0", null, null, null);
 ...
 Cursor c2 = getContentResolver().query(rawContactUri,
          RawContacts.DELETED + "<>0", null, null, null);
 

读取原始联系人以及与其关联的所有数据的最佳方式是使用ContactsContract.RawContacts.Entity目录。 如果原始联系人具有数据行,则实体游标将为每个数据行包含一行。 如果原始联系人没有数据行,则光标仍将包含一行与原始联系人级别信息。

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

Columns

RawContacts
long _ID read-only 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.
long CONTACT_ID read-only 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 ContactsContract.AggregationExceptions.
int AGGREGATION_MODE read/write 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 ContactsContract.AggregationExceptions.
int DELETED read/write The "deleted" flag: "0" by default, "1" if the row has been marked for deletion. When delete(Uri, String, String[]) 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 CALLER_IS_SYNCADAPTER query parameter to finalize the data removal.
int TIMES_CONTACTED read/write The number of times the contact has been contacted. 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. After that, this value is typically updated via markAsContacted(ContentResolver, long).
long LAST_TIME_CONTACTED read/write The timestamp of the last time the contact was contacted. 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. After that, this value is typically updated via markAsContacted(ContentResolver, long).
int STARRED read/write 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.
String CUSTOM_RINGTONE read/write A custom ringtone associated with a raw contact. Typically this is the URI returned by an activity launched with the 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 Contacts.CUSTOM_RINGTONE instead.
int SEND_TO_VOICEMAIL read/write 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.
String ACCOUNT_NAME read/write-once 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.
String ACCOUNT_TYPE read/write-once

此行所属的帐户类型,与ACCOUNT_NAME配对时ACCOUNT_NAME识别特定帐户。 它应该在原始联系人插入时设置,并且之后不会更改。

为确保唯一性,应根据Java包命名约定选择新帐户类型。 因此,Google帐户的类型为“com.google”。

String DATA_SET read/write-once

该行属于该帐户内的数据集。 这允许同一账户类型的多个同步适配器区分彼此的数据。 的组合ACCOUNT_TYPEACCOUNT_NAME ,和DATA_SET识别一组与单个同步适配器相关联的数据。

这在默认情况下是空的,并且是完全可选的。 如果多个同步适配器针对相同的帐户类型和帐户名称输入不同的数据,则只需填充它。

它应该在原始联系人插入时设置,并且之后不会更改。

String SOURCE_ID read/write 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.
int VERSION read-only 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.
int DIRTY read/write 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 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.
String SYNC1 read/write 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.
String SYNC2 read/write Generic column for use by sync adapters.
String SYNC3 read/write Generic column for use by sync adapters.
String SYNC4 read/write Generic column for use by sync adapters.

Summary

Nested classes

class ContactsContract.RawContacts.Data

包含所有ContactsContract.Data行的单个原始联系人的ContactsContract.Data

class ContactsContract.RawContacts.DisplayPhoto

代表其主要显示照片的单个原始联系人的子目录。

class ContactsContract.RawContacts.Entity

包含所有ContactsContract.Data行的单个原始联系人的ContactsContract.Data

Constants

int AGGREGATION_MODE_DEFAULT

聚合模式:插入或更新操作完成后立即聚合。

int AGGREGATION_MODE_DISABLED

聚合模式:永远不会聚合此原始联系人。

int AGGREGATION_MODE_IMMEDIATE

此常数在API级别11中已被弃用。聚合是同步的,这个历史值是无操作的

int AGGREGATION_MODE_SUSPENDED

聚合模式:聚合暂时暂停,并可能稍后恢复。

String CONTENT_ITEM_TYPE

将原始联系人ID附加到 CONTENT_URI时生成一个人的子目录的结果的MIME类型。

String CONTENT_TYPE

未提供特定ID值时,来自 CONTENT_URI的结果的MIME类型,并且可能会返回多个原始联系人。

Inherited constants

From interface android.provider.BaseColumns
From interface android.provider.ContactsContract.RawContactsColumns
From interface android.provider.ContactsContract.ContactOptionsColumns
From interface android.provider.ContactsContract.ContactNameColumns
From interface android.provider.ContactsContract.SyncColumns
From interface android.provider.ContactsContract.BaseSyncColumns

Fields

public static final Uri CONTENT_URI

该表格的内容://样式URI,用于请求与选择标准匹配的原始联系人行的目录。

Public methods

static Uri getContactLookupUri(ContentResolver resolver, Uri rawContactUri)

为给定的 ContactsContract.RawContacts条目的父级 ContactsContract.Contacts条目创建一个 CONTENT_LOOKUP_URI样式 Uri

static EntityIterator newEntityIterator(Cursor cursor)

TODO:javadoc

Inherited methods

From class java.lang.Object

Constants

AGGREGATION_MODE_DEFAULT

Added in API level 5
int AGGREGATION_MODE_DEFAULT

聚合模式:插入或更新操作完成后立即聚合。

常量值:0(0x00000000)

AGGREGATION_MODE_DISABLED

Added in API level 5
int AGGREGATION_MODE_DISABLED

聚合模式:永远不会聚合此原始联系人。 原始联系人不会有相应的Contacts聚合,因此不会包含在Contacts查询结果中。

例如,此模式可用于在等待删除发生在服务器端时标记为删除的原始联系人。

也可以看看:

常量值:3(0x00000003)

AGGREGATION_MODE_IMMEDIATE

Added in API level 5
int AGGREGATION_MODE_IMMEDIATE

此常数在API级别11中已弃用。
聚合是同步的,这个历史价值是没有操作的

聚合模式:插入/更新原始联系时的聚合模式。

常数值:1(0x00000001)

AGGREGATION_MODE_SUSPENDED

Added in API level 5
int AGGREGATION_MODE_SUSPENDED

聚合模式:聚合暂时暂停,并可能稍后恢复。 原始联系人的更改将更新关联的汇总联系人,但不会导致联系人汇总方式的任何更改。 类似于AGGREGATION_MODE_DISABLED ,但保持链接到相应的Contacts聚合。

这可以用于推迟聚合,直到一系列更新之后,才能获得更好的性能和/或用户体验。

请注意,将 AGGREGATION_MODEAGGREGATION_MODE_SUSPENDED更改为 AGGREGATION_MODE_DEFAULT不会触发聚合过程,但对原始联系人数据的任何后续更改都将会发生。

常量值:2(0x00000002)

CONTENT_ITEM_TYPE

Added in API level 5
String CONTENT_ITEM_TYPE

将原始联系人ID附加到 CONTENT_URI时生成一个人的子目录的结果的MIME类型。

常量值:“vnd.android.cursor.item / raw_contact”

CONTENT_TYPE

Added in API level 5
String CONTENT_TYPE

未提供特定ID值时,来自 CONTENT_URI的结果的MIME类型,并且可能返回多个原始联系人。

常量值:“vnd.android.cursor.dir / raw_contact”

Fields

CONTENT_URI

Added in API level 5
Uri CONTENT_URI

该表格的内容://样式URI,用于请求与选择标准匹配的原始联系人行的目录。

Public methods

getContactLookupUri

Added in API level 5
Uri getContactLookupUri (ContentResolver resolver, 
                Uri rawContactUri)

为给定的 ContactsContract.RawContacts条目的父级 ContactsContract.Contacts条目创建一个 CONTENT_LOOKUP_URI样式 Uri

Parameters
resolver ContentResolver
rawContactUri Uri
Returns
Uri

newEntityIterator

Added in API level 8
EntityIterator newEntityIterator (Cursor cursor)

TODO:javadoc

Returns
EntityIterator

Hooray!