[android-developers] Add contact to a group contacts

2012-06-14 Thread Ricardo Graça


Hi Everybody.

I need to insert a contact in a contact group. I use the content provider 
contactsContract of android.

The group is added, but the contact is not added in this group. Can you 
help me? Here is my code. 

Thanks

ops = new ArrayList();
ops.add(ContentProviderOperation
   .newInsert(ContactsContract.RawContacts.CONTENT_URI)
   .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, accountType)
   .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, accountName)
   .build());   

ContentResolver resolver = getContentResolver();
ops.add(ContentProviderOperation
   .newAssertQuery(ContactsContract.Groups.CONTENT_URI)
   .withSelection(ContactsContract.Groups.TITLE + "=?", new String[]{grupo})
   .withExpectedCount(0)
   .build()
);
ops.add(ContentProviderOperation
   .newInsert(ContactsContract.Groups.CONTENT_URI)
   .withValue(ContactsContract.Groups.TITLE, grupo)
   .withValue(ContactsContract.Groups.GROUP_VISIBLE, true)
   .withValue(ContactsContract.Groups.ACCOUNT_NAME, accountName)
   .withValue(ContactsContract.Groups.ACCOUNT_TYPE, accountType)
   .build());

try {
resolver.applyBatch(ContactsContract.AUTHORITY, ops);
} catch (RemoteException ex){
Log.e(AbstractEditActivity.class.getName(), ex.getMessage(), ex);
} catch (OperationApplicationException ex) {
Log.w(AbstractEditActivity.class.getName(), ex.getMessage());
}

ops.clear();
ops = new ArrayList();
ops.add(ContentProviderOperation
   .newAssertQuery(ContactsContract.Groups.CONTENT_URI) 
   .withValue(ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID, 0)
   .withSelection(ContactsContract.Groups.TITLE + "=?", new String[]{grupo})
   .withExpectedCount(1)
   .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, 
nome)
   .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(Data.MIMETYPE, 
ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
   .withValue(ContactsContract.CommonDataKinds.Email.DATA, mail)
   .build());
ops.add(ContentProviderOperation
   .newInsert(ContactsContract.Data.CONTENT_URI)
   .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
   .withValue(ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID, 0)
   .withValue(ContactsContract.CommonDataKinds.GroupMembership.MIMETYPE,
   ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE)
   .build());  
try{
resolver.applyBatch(ContactsContract.AUTHORITY, ops);
}catch(Exception e){
e.printStackTrace();
}   

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Re: [android-developers] Add contact to a group contacts

2012-06-14 Thread asheesh arya
just go through this link might you got some idea!!
http://www.pocketmagic.net/?p=1844

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en