tags:

views:

565

answers:

3

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 collecting info. of users to write, store it in the ArrayList 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 .

A: 

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 !

Karan
In this example, where are account.name, account.type and username coming from?
Chris
You can use the account name and type when you are using any specific account (i.e if you have synced your gmail account and want to add one more contact in that). Sync1 option is the user name of the account, it is used to store the local sync information.
Karan
A: 

I run the same above code but nothing is added in my contact book why? can you please me pleaxe

PritiJinny
A: 

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, "My 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().applyBatchContactsContract.AUTHORITY, op_list);
} catch (OperationApplicationException exp) {
    exp.printStackTrace();
} catch (RemoteException exp) {
    exp.printStackTrace();
}
mob_king