android.provider.ContactsContract.RawContacts

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.

Aggregation

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.

Operations
Insert

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

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, "Mike Sullivan");
getContentResolver().insert(Data.CONTENT_URI, values);

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<ContentProviderOperation> ops = Lists.newArrayList();
...
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);

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.

Update

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.

Delete

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.

Query

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

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

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

Columns

RawContacts

类型

字段名字

读写权限

备注

long

_ID

只读

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

只读

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.

int

AGGERATION_MODE

可读/可写

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.

int

DELETED

可读/可写

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.

int

TIMES_CONTACTED

可读/可写

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 ContactsContract.Contacts.markAsContacted.

long

LAST_TIME_CONTACTED

可读/可写

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 ContactsContract.Contacts.markAsContacted.

int

STARRED

可读/可写

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

可读/可写

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 Contacts.CUSTOM_RINGTONE instead.

int

SEND_TO_VOICEMAIL

可读/可写

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

可读/写一次

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

可读/写一次

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".

String

SOURCE_ID

可读/可写

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

只读

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

可读/可写

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.

String

SYNC1

可读/可写

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

可读/可写

Generic column for use by sync adapters.

String

SYNC3

可读/可写

Generic column for use by sync adapters.

String

SYNC4

可读/可写

Generic column for use by sync adapters.

Android API之android.provider.ContactsContract.RawContacts的更多相关文章

  1. Android API之android.provider.ContactsContract

    android.provider.ContactsContract ContactsContract是联系人provider和app的contract.定义了已支持的URL和column.取代了之前的 ...

  2. Android API之android.provider.ContactsContract.Data

    android.provider.ContactsContract.Data Constants for the data table, which contains data points tied ...

  3. Android API之android.provider.ContactsContract.Contacts

    android.provider.ContactsContract.Contacts 对应contacts数据表.RawContacts的一个聚合(aggregate)代表同一个人.每个人在数据表co ...

  4. 【Android API】Android 4.1 API官方文档详解

    原文:http://android.eoe.cn/topic/summary 翻译:[eoeAndroid原创团队]kris.流风而逝.贼寇在何方.snowxwyo.lsy4833406 更新日期:2 ...

  5. Android API之android.content.BroadcastReceiver

    android.content.BroadcastReceiver Base class for code that will receive intents sent by sendBroadcas ...

  6. Android API之android.os.Parcelable

    android.os.Parcelable Interface for classes whose instances can be written to and restored from a Pa ...

  7. Android API之android.widget.Filterable

      android.widget.Filterable 定义了一种可过滤的行为.Filterable接口通常有android.widget.Adapter来实现.接口Filterable中有个抽象方法 ...

  8. Android API之android.content.AsyncQueryHandler

    android.content.AsyncQueryHandler A helper class to help make handling asynchronous ContentResolver ...

  9. Android API之android.view.View.MeasureSpec

    android.view.View.MeasureSpec MeasureSpec是View的内部类 public static class MeasureSpec MeasureSpec封装从par ...

随机推荐

  1. .NET:异常以及异常处理框架探析(转载)

    概述 一般情况下,企业级应用都对应着复杂的业务逻辑,为了保证系统的健壮,必然需要面对各种系统业务异常和运行时异常. 不好的异常处理方式容易造成应用程序逻辑混乱,脆弱而难于管理.应用程序中充斥着零散的异 ...

  2. 《Unix内核源码剖析》

    <Unix内核源码剖析> 基本信息 作者: (日)青柳隆宏 译者: 殷中翔 丛书名: 图灵程序设计丛书 出版社:人民邮电出版社 ISBN:9787115345219 上架时间:2014-2 ...

  3. bashrc和profile的用途和区别

    使用终端登录Linux操作系统的控制台后,会出现一个提示符号(例如:#或~),在这个提示符号之后可以输入命令,Linux根据输入的命令会做回应,这一连串的动作是由一个所谓的Shell来做处理. She ...

  4. Oracle中表列由VARCHAR2类型改成CLOB

    情景 原来表中的列定义成VARCHAR2类型,众所周知,VARCHAR2类型最大支持长度为4000.假设因为业务须要.想把此列转换为CLOB类型,在Oracle中直接通过ALTER语句转换是行不通的. ...

  5. Mongodb安全认证及Java调用

    Mongodb安全认证在单实例和副本集两种情况下不太一样,单实例相对简单,只要在启动时加上 --auth参数即可,但副本集则需要keyfile. 一.单实例 1.启动服务(先不要加auth参数) 2. ...

  6. 【IUML】支持向量机SVM[续]

    支持向量机基本上是最好的有监督学习算法了.看很多正统的讲法都是从VC 维理论和结构风险最小原理出发,然后引出SVM什么的,还有些资料上来就讲分类超平面什么的.我们logistic回归出发,引出了SVM ...

  7. jQuery的搜索关键词自动匹配插件

    相信许多人都会用过搜索栏自动匹配关键词的功能,无论是像google的专业搜索引擎,还是普通的网站,现在许多都用上了这种关键词匹配技术,本文介绍的用jQuery实现的关键词匹配技术,当然要整合到自己的系 ...

  8. ACM:图的BFS,走迷宫

    题目: 一个网格迷宫由n行m列的单元格组成,每一个单元格要么是空地(用1表示),要么是障碍物(用0来表示).你的任务是找一条从起点到终点的最短移动序列,当中UDLR分别表示往上.下.左.右移动到相邻单 ...

  9. Oracle中的数值处理方法

    求绝对值函数 ) from dual; 求平方根函数 ) from dual; 求幂函数 ,3) from dual; 求余弦三角函数 select cos(3.14159) from dual; 求 ...

  10. 升级mojave后的小问题解决

    首先是xcode没了,我到苹果软件市场上重新下载了一个,可以了. 然后是virtualbox无法打开了,现实版本不兼容,我到官网重新下载了一个最新的6.0.然后就可以正常打开了,并且是无感升级,原来的 ...