Android API之android.provider.ContactsContract.Data
android.provider.ContactsContract.Data
Constants for the data table, which contains data points tied to a raw contact. Each row of the data table is typically used to store a single piece of contact information (such as a phone number) and its associated metadata (such as whether it is a work or home number).
Data kinds
Data is a generic table that can hold any kind of contact data. The kind of data stored in a given row is specified by the row's MIMETYPE
value, which determines the meaning of the generic columns DATA1
through DATA15
. For example, if the data kind is Phone.CONTENT_ITEM_TYPE
, then the column DATA1
stores the phone number, but if the data kind is Email.CONTENT_ITEM_TYPE
, then DATA1
stores the email address. Sync adapters and applications can introduce their own data kinds.
ContactsContract defines a small number of pre-defined data kinds, e.g. CommonDataKinds.Phone
, CommonDataKinds.Email
etc. As a convenience, these classes define data kind specific aliases for DATA1 etc. For example, Phone.NUMBER
is the same as Data.DATA1
.
DATA1
is an indexed column and should be used for the data element that is expected to be most frequently used in query selections. For example, in the case of a row representing email addresses DATA1
should probably be used for the email address itself, while DATA2
etc can be used for auxiliary information like type of email address.
By convention, DATA15
is used for storing BLOBs (binary data).
The sync adapter for a given account type must correctly handle every data type used in the corresponding raw contacts. Otherwise it could result in lost or corrupted data.
Similarly, you should refrain from introducing new kinds of data for an other party's account types. For example, if you add a data row for "favorite song" to a raw contact owned by a Google account, it will not get synced to the server, because the Google sync adapter does not know how to handle this data kind. Thus new data kinds are typically introduced along with new account types, i.e. new sync adapters.
Batch operations
Data rows can be inserted/updated/deleted using the traditional ContentResolver.insert
, ContentResolver.update
and ContentResolver.delete
methods, however the newer mechanism based on a batch of ContentProviderOperation
will prove to be a better choice in almost all cases. All operations in a batch are executed in a single transaction, which ensures that the phone-side and server-side state of a raw contact are always consistent. Also, the batch-based approach is far more efficient: not only are the database operations faster when executed in a single transaction, but also sending a batch of commands to the content provider saves a lot of time on context switching between your process and the process in which the content provider runs.
The flip side of using batched operations is that a large batch may lock up the database for a long time preventing other applications from accessing data and potentially causing ANRs ("Application Not Responding" dialogs.)
To avoid such lockups of the database, make sure to insert "yield points" in the batch. A yield point indicates to the content provider that before executing the next operation it can commit the changes that have already been made, yield to other requests, open another transaction and continue processing operations. A yield point will not automatically commit the transaction, but only if there is another request waiting on the database. Normally a sync adapter should insert a yield point at the beginning of each raw contact operation sequence in the batch. See ContentProviderOperation.Builder.withYieldAllowed(boolean)
.
Operations
- Insert
-
An individual data row can be inserted using the traditional
ContentResolver.insert(Uri, ContentValues)
method. Multiple rows should always be inserted as a batch.An example of a traditional insert:
ContentValues values = new ContentValues(); values.put(Data.RAW_CONTACT_ID, rawContactId); values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE); values.put(Phone.NUMBER, "1-800-GOOG-411"); values.put(Phone.TYPE, Phone.TYPE_CUSTOM); values.put(Phone.LABEL, "free directory assistance"); Uri dataUri = getContentResolver().insert(Data.CONTENT_URI, values);
The same done using ContentProviderOperations:
ArrayList<ContentProviderOperation> ops = Lists.newArrayList(); ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI) .withValue(Data.RAW_CONTACT_ID, rawContactId) .withValue(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE) .withValue(Phone.NUMBER, "1-800-GOOG-411") .withValue(Phone.TYPE, Phone.TYPE_CUSTOM) .withValue(Phone.LABEL, "free directory assistance") .build()); getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
- Update
-
Just as with insert, update can be done incrementally or as a batch, the batch mode being the preferred method:
ArrayList<ContentProviderOperation> ops = Lists.newArrayList(); ops.add(ContentProviderOperation.newUpdate(Data.CONTENT_URI) .withSelection(Data._ID + "=?", new String[]{String.valueOf(dataId)}) .withValue(Email.DATA, "somebody@android.com") .build()); getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
- Delete
-
Just as with insert and update, deletion can be done either using the
ContentResolver.delete
method or using a ContentProviderOperation:ArrayList<ContentProviderOperation> ops = Lists.newArrayList(); ops.add(ContentProviderOperation.newDelete(Data.CONTENT_URI) .withSelection(Data._ID + "=?", new String[]{String.valueOf(dataId)}) .build()); getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
- Query
- Finding all Data of a given type for a given contact
-
Cursor c = getContentResolver().query(Data.CONTENT_URI, new String[] {Data._ID, Phone.NUMBER, Phone.TYPE, Phone.LABEL}, Data.CONTACT_ID + "=?" + " AND " + Data.MIMETYPE + "='" + Phone.CONTENT_ITEM_TYPE + "'", new String[] {String.valueOf(contactId)}, null);
- Finding all Data of a given type for a given raw contact
-
Cursor c = getContentResolver().query(Data.CONTENT_URI, new String[] {Data._ID, Phone.NUMBER, Phone.TYPE, Phone.LABEL}, Data.RAW_CONTACT_ID + "=?" + " AND " + Data.MIMETYPE + "='" + Phone.CONTENT_ITEM_TYPE + "'", new String[] {String.valueOf(rawContactId)}, null);
- Finding all Data for a given raw contact
- Most sync adapters will want to read all data rows for a raw contact along with the raw contact itself. For that you should use the
RawContactsEntity
. See alsoRawContacts
.
Columns
Many columns are available via a Data.CONTENT_URI
query. For best performance you should explicitly specify a projection to only those columns that you need.
Data
类型 |
字段名称 |
读写权限 |
备注 |
long |
_ID |
只读 |
Row ID. Sync adapter should try to preserve row IDs during updates. In other words, it would be a bad idea to delete and reinsert a data row. A sync adapter should always do an update instead. |
String |
MIMETYPE |
可读/只写一次 |
The MIME type of the item represented by this row. Examples of common MIME types are:
|
long |
RAW_CONTACT_ID |
可读/可写一次 |
The id of the row in the |
int |
IS_PRIMARY |
可读/可写 |
Whether this is the primary entry of its kind for the raw contact it belongs to. "1" if true, "0" if false. |
int |
IS_SUPER_PRIMARY |
可读/可写 |
Whether this is the primary entry of its kind for the aggregate contact it belongs to. Any data record that is "super primary" must also be "primary". For example, the super-primary entry may be interpreted as the default contact value of its kind (for example, the default phone number to use for the contact). |
int |
DATA_VERSION |
只读 |
The version of this data record. Whenever the data row changes the version goes up. This value is monotonically increasing. |
Any type |
|
可读/可写 |
Generic data columns. The meaning of each column is determined by the Data columns whose meaning is not explicitly defined for a given MIMETYPE should not be used. There is no guarantee that any sync adapter will preserve them. Sync adapters themselves should not use such columns either, but should instead use |
Any type |
可读/可写 |
Generic columns for use by sync adapters. For example, a Photo row may store the image URL in SYNC1, a status (not loaded, loading, loaded, error) in SYNC2, server-side version number in SYNC3 and error code in SYNC4. Some columns from the most recent associated status update are also available through an implicit join. Join with |
|
int |
PRESENCE |
只读 |
IM presence status linked to this data row. Compare with CONTACT_PRESENCE , which contains the contact's presence across all IM rows. See StatusUpdates for individual status definitions. The provider may choose not to store this value in persistent storage. The expectation is that presence status will be updated on a regular basic. |
String |
STATUS |
只读 |
Latest status update linked with this data row. |
long |
STATUS_TIMESTAMP |
只读 |
The absolute time in milliseconds when the latest status was inserted/updated for this data row. |
String |
STATUS_RES_PACKAGE |
只读 |
The package containing resources for this status: label and icon. |
long |
STATUS_LABLE |
只读 |
The resource ID of the label describing the source of status update linked to this data row. This resource is scoped by the STATUS_RES_PACKAGE . |
long |
STATUS_ICON |
只读 |
The resource ID of the icon for the source of the status update linked to this data row. This resource is scoped by the Some columns from the associated raw contact are also available through an implicit join. The other columns are excluded as uninteresting in this context. Join with |
long |
CONTACT_ID |
只读 |
The id of the row in the Contacts table that this data belongs to. |
int |
AGGREGATION_MODE |
只读 |
See RawContacts . |
int |
DELETED |
只读 |
See The ID column for the associated aggregated contact table Join with |
String |
LOOKUP_KEY |
只读 |
See ContactsContract.Contacts |
String |
DISPLAY_NAME |
只读 |
See ContactsContract.Contacts |
long |
PHOTO_ID |
只读 |
See ContactsContract.Contacts |
int |
IN_VISIBLE_GROUP |
只读 |
See ContactsContract.Contacts |
int |
HAS_PHONE_NUMBER |
只读 |
See ContactsContract.Contacts |
int |
TIMES_CONTACTED |
只读 |
See ContactsContract.Contacts |
long |
LAST_TIME_CONTACTED |
只读 |
See ContactsContract.Contacts |
int |
STARRED |
只读 |
See ContactsContract.Contacts |
String |
CUSTOM_RINGTONE |
只读 |
See ContactsContract.Contacts |
int |
SEND_TO_VOICEMAIL |
只读 |
See ContactsContract.Contacts |
int |
CONTACT_PRESENCE |
只读 |
See ContactsContract.Contacts |
String |
CONTACT_STATUS |
只读 |
See ContactsContract.Contacts |
long |
CONTACT_STATUS_TIMESTAMP |
只读 |
See ContactsContract.Contacts |
String |
CONTACT_STATUS_RES_PACKAGE |
只读 |
See ContactsContract.Contacts |
long |
CONTACT_STATUS_LABEL |
只读 |
See ContactsContract.Contacts |
long |
CONTACT_STATUS_ICON |
只读 |
See ContactsContract.Contacts |