How to add a contact to the Android group

I have the following code to add a contact to a group in an app / people application for an android application, it adds a group, but not a contact in this group, what am I missing? I add a contact successfully, also creating a group, I get the identifiers of both things, I use the following code to associate a contact with a group, but it does not work, the group is always empty.

public Uri addToGroup(long personId, long groupId) { ContentValues values = new ContentValues(); values.put(ContactsContract.CommonDataKinds.GroupMembership.RAW_CONTACT_ID, personId); values.put( ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID, groupId); values .put( ContactsContract.CommonDataKinds.GroupMembership.MIMETYPE, ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE); return this.getActivity().getContentResolver().insert( ContactsContract.Data.CONTENT_URI, values); } 

**** update ***** Another thing I found is that the group I created does not sync with Google, perhaps the reason is that the contacts are not added.

+7
java android contacts
source share
2 answers

Finally, you can add a contact to the group, this is what was needed, create a contact that synchronizes with the google account (required), second create a group that can synchronize with the default synchronization service, and then add the contact as I add over the code.

if you're interested in learning how to create a group that can sync, here it is

 public String createGroup(String name) { String[] GROUP_PROJECTION = new String[] { ContactsContract.Groups._ID, ContactsContract.Groups.TITLE }; try { ContentValues groupValues = null; ContentResolver cr = this.getContentResolver(); groupValues = new ContentValues(); groupValues.put(ContactsContract.Groups.TITLE, name); groupValues.put(ContactsContract.Groups.SHOULD_SYNC,true); cr.insert(ContactsContract.Groups.CONTENT_URI, groupValues); } catch(Exception e){ Log.d("########### Exception :",""+e.getMessage()); return "1"; } String groupID = null; Cursor getGroupID_Cursor = null; getGroupID_Cursor = this.getContentResolver().query(ContactsContract.Groups.CONTENT_URI, GROUP_PROJECTION, ContactsContract.Groups.TITLE+ "=?", new String[]{name}, null); getGroupID_Cursor.moveToFirst(); groupID = (getGroupID_Cursor.getString(getGroupID_Cursor.getColumnIndex("_id"))); return groupID; } 
+2
source share

Use ContentProviderOperation for this.

 ArrayList<ContentProviderOperation> operationList = new ArrayList<ContentProviderOperation>(); 

If a group exists with groupId ,

 operationList.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) .withValue(ContactsContract.CommonDataKinds.GroupMembership.MIMETYPE, ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE) .withValue(ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID, groupId).build()); 

If the group does not exist:

 // create group and insert ContentValues groupValues; ContentResolver cr = context.getContentResolver(); groupValues = new ContentValues(); groupValues.put(ContactsContract.Groups.TITLE, newGroupId); try { cr.insert(ContactsContract.Groups.CONTENT_URI, groupValues); } catch (Exception e) { // handle } operationList.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) .withValue(ContactsContract.CommonDataKinds.GroupMembership.MIMETYPE, ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE) .withValue(ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID, newGroupId).build()); 

And, apply the changes:

 ContentProviderResult[] cpr = context.getContentResolver().applyBatch(ContactsContract.AUTHORITY, operationList); 
+1
source share

All Articles