Skip to content

Instantly share code, notes, and snippets.

@marvinlix
Created April 19, 2012 13:57
Show Gist options
  • Select an option

  • Save marvinlix/2421133 to your computer and use it in GitHub Desktop.

Select an option

Save marvinlix/2421133 to your computer and use it in GitHub Desktop.
add contacts in android2.2
/*
主要数据有三张表:contacts, raw_contacts,data。
contacts:主要是raw_contacts的一个合并。
raw_contacts:是每条记录表示一条联系人。
data:最基本的表,其中包含所有联系人的数据。每条记录都有一个mime type代表该记录的类型。
*/
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
.withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, "com.android.exchange")
.withValue(ContactsContract.RawContacts.ACCOUNT_NAME, "xxxxxxxxxx@gmail.com")
.build());
ops.add(ContentProviderOperation
.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, "xxxxxxx")
.build());
ops.add(ContentProviderOperation
.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, "10086")
.withValue(ContactsContract.CommonDataKinds.Phone.TYPE, 1)
.build());
ops.add(ContentProviderOperation
.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Email.DATA, "xxxxxx@qq.com")
.withValue(ContactsContract.CommonDataKinds.Email.TYPE, 2)
.build());
try {
getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
} catch (Exception e) {
// Display warning
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment