/**
* 新建联系人的接口
*
* @param String
* accountName,accountType 为账号名账号类型,一般为NULL
* @throws RemoteException
* @throws OperationApplicationException
*/
publicstaticString _insertContact(ContentResolver cr, String accountName,
String accountType, String displayName, ArrayList phone,
ArrayList email, ArrayList im,
ArrayList address, ArrayList organization,
ArrayList notes, ArrayList nickname,
ArrayList website)throwsRemoteException,
OperationApplicationException {
ArrayList ops =newArrayList();
String rawId ="";
longrawContactId = insertRawContact(cr, accountName, accountType);
rawId = Long.toString(rawContactId);
if(displayName !=null) {
insertContactDisplayname(ops, StructuredName.CONTENT_ITEM_TYPE,
rawId, displayName);
}
if(phone !=null) {
for(intj =0; j < phone.size(); j++) {
String[] item = phone.get(j);
insertItemToContact(ops, Phone.CONTENT_ITEM_TYPE, rawId,
PROJECTION_PHONENUMBER_CONTACT, item);
}
}
if(email !=null) {
for(intj =0; j < email.size(); j++) {
String[] item = email.get(j);
insertItemToContact(ops, Email.CONTENT_ITEM_TYPE, rawId,
PROJECTION_EAMIL_CONTACT, item);
}
}
if(im !=null) {
for(intj =0; j < im.size(); j++) {
String[] item = im.get(j);
insertItemToContact(ops, Im.CONTENT_ITEM_TYPE, rawId,
PROJECTION_IM_CONTACT, item);
}
}
if(address !=null) {
for(intj =0; j < address.size(); j++) {
String[] item = address.get(j);
insertItemToContact(ops, StructuredPostal.CONTENT_ITEM_TYPE,
rawId, PROJECTION_ADDRESS_CONTACT, item);
}
}
if(organization !=null) {
for(intj =0; j < organization.size(); j++) {
String[] item = organization.get(j);
insertItemToContact(ops, Organization.CONTENT_ITEM_TYPE, rawId,
PROJECTION_ORGANIZATION_CONTACT, item);
}
}
if(notes !=null) {
for(intj =0; j < notes.size(); j++) {
String[] item = notes.get(j);
insertItemToContact(ops, Note.CONTENT_ITEM_TYPE, rawId,
PROJECTION_NOTES_CONTACT, item);
}
}
if(nickname !=null) {
for(intj =0; j < nickname.size(); j++) {
String[] item = nickname.get(j);
insertItemToContact(ops, Nickname.CONTENT_ITEM_TYPE, rawId,
PROJECTION_NICKNAMES_CONTACT, item);
}
}
if(website !=null) {
for(intj =0; j < website.size(); j++) {
String[] item = website.get(j);
insertItemToContact(ops, Website.CONTENT_ITEM_TYPE, rawId,
PROJECTION_WEBSITES_CONTACT, item);
}
}
cr.applyBatch(ContactsContract.AUTHORITY, ops);
returnrawId;
}
/*
* 通过往ROWCONTACT里插入数据,获得rawId
*
* @param cr
*
* @param accountName 一般为NULL
*
* @param accountType 一般为NULL
*
* @return
*/
privatestaticlonginsertRawContact(ContentResolver cr,
String accountName, String accountType) {
ContentValues values =newContentValues();
values.put(RawContacts.ACCOUNT_NAME, accountName);
values.put(RawContacts.ACCOUNT_TYPE, accountType);
// values.put(Contacts.DISPLAY_NAME, displayName);
Uri rawContactUri = cr.insert(RawContacts.CONTENT_URI, values);
longrawContactId = ContentUris.parseId(rawContactUri);
returnrawContactId;
}
privatestaticvoidinsertContactDisplayname(
ArrayList ops, String mimeType,
String rawContactId, String displayName)throwsRemoteException,
OperationApplicationException {
ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI).withValue(
Data.MIMETYPE, mimeType).withValue(Data.RAW_CONTACT_ID,
rawContactId).withValue(StructuredName.DISPLAY_NAME,
displayName).build());
}
privatestaticvoidinsertItemToContact(
ArrayList ops, String mimeType,
String rawContactId, String[] PROJECTION_CONTACT, String[] item)
throwsRemoteException, OperationApplicationException {
// ContentValues values = new ContentValues();
// values.put(Data.RAW_CONTACT_ID, rawContactId);
// values.put(Data.MIMETYPE, mimeType);
// for (int i = 0; i < PROJECTION_CONTACT.length; i++) {
// values.put(PROJECTION_CONTACT[i], item[i]);
// }
// Uri dataUri = cr.insert(Data.CONTENT_URI, values);
Builder builder = ContentProviderOperation.newInsert(Data.CONTENT_URI);
builder.withYieldAllowed(true);
builder.withValue(Data.RAW_CONTACT_ID, rawContactId);
builder.withValue(Data.MIMETYPE, mimeType);
for(inti =0; i < PROJECTION_CONTACT.length; i++) {
builder.withValue(PROJECTION_CONTACT[i], item[i]);
}
ops.add(builder.build());
}