1

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<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 .

Chillie
  • 1,030
  • 10
  • 28
Pritam
  • 2,367
  • 5
  • 34
  • 50
  • Solved this with the help from new article http://developer.android.com/resources/articles/contacts.html – Pritam Apr 22 '10 at 04:14

2 Answers2

1

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
  • 12,724
  • 6
  • 40
  • 33
  • In this example, where are account.name, account.type and username coming from? – Chris Sep 10 '10 at 17:14
  • 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 Sep 13 '10 at 07:28
0

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();
}
Roohi Zuwairiyah
  • 363
  • 3
  • 15
Pritam
  • 2,367
  • 5
  • 34
  • 50