Can anyone please help me on adding new contacts in address book and apply them in batch by using new ContactsContract API? I could not find a proper tutorial on this.
I am able to add a single contact. But batch update fails with Unknown contacts being added.
Currently I am looping through while loop while c开发者_如何学运维ollecting info. of users to write, store it in the ArrayList<ContentProviderOperation>
and applying and
ContentProviderResult[] result = getContentResolver().applyBatch(ContactsContract.AUTHORITY, op_list);
But only one contact is updated with name and other are updated as unknown contacts.
Please help with a sample code which adds the fields like name,nickname,mobile,title,email,Skype id,work-country etc.
Any help ? Thanks .
Following code will add the RawContact entry and then add the name. For adding any other field use the similar code that is used for adding Name with proper values.
// Raw Contact
ContentProviderOperation.Builder builder = ContentProviderOperation.newInsert(RawContacts.CONTENT_URI);
builder.withValue(RawContacts.ACCOUNT_NAME, account.name);
builder.withValue(RawContacts.ACCOUNT_TYPE, account.type);
builder.withValue(RawContacts.SYNC1, username);
operationList.add(builder.build());
// Name
builder = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI);
builder.withValueBackReference(ContactsContract.CommonDataKinds.StructuredName.RAW_CONTACT_ID, 0);
builder.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE);
builder.withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, entry.getName().getFullName().getValue() );
operationList.add(builder.build());
try {
mContentResolver.applyBatch(ContactsContract.AUTHORITY, operationList);
} catch (Exception e) {
e.printStackTrace();
}
HTH !
This is my code that worked, you can add the fields as you require for other values:
int backRefIndex = 0
ArrayList<ContentProviderOperation> op_list = new ArrayList<ContentProviderOperation>();
op_list.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI).withValue(RawContacts.ACCOUNT_TYPE, null).withValue(RawContacts.ACCOUNT_NAME, null)
.build());
op_list.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI).withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, backRefIndex).withValue(ContactsContract.Data.MIMETYPE,ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE).withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, "Contact_name").build());
op_list.add(ContentProviderOperation.newInsert(Data.CONTENT_URI).withValueBackReference(Data.RAW_CONTACT_ID,backRefIndex).withValue(Phone.MIMETYPE,Phone.CONTENT_ITEM_TYPE).withValue(Phone.NUMBER,"1234567890").withValue(Phone.TYPE,Phone.TYPE_MOBILE).withValue(Phone.TYPE, Phone.TYPE_WORK).build());
try {
ContentProviderResult[] result = context.getContentResolver().applyBatch(ContactsContract.AUTHORITY, op_list);
} catch (OperationApplicationException exp) {
exp.printStackTrace();
} catch (RemoteException exp) {
exp.printStackTrace();
}
精彩评论