Search code examples
androidcontactscontactscontractaccounts

How to get Contact Groups into Contact Manager Sample App?


I am running the Contact Manager sample app from the Android SDK. When I add a contact to my Gmail account, it gets added as an 'invisible contact'. I am assuming this is because I am not telling the contact which 'group' it should be assigned to. I have been looking around the internet for a few days and have come up empty handed.

What I really want to do is add the contact to the Contact Account that I select and have the contact associated with a Contact Group within the selected Gmail Account, so the contact's info will be displayed in the user's contacts.


Solution

  • What version of Android are you targeting? You are of course aware that the Contacts API changed radically in 2.x...

    I hit this exact problem of invisible contacts, but only on 1.x. I found the solution was to add to the built-in "My Contacts" group :

    // Add to the My Contacts group
    ContentValues values = new ContentValues();
    values.put(GroupMembership.PERSON_ID, contact.mAndroidId); 
    values.put(GroupMembership.GROUP_ID, 1); // 1 is always the ID of the built-in "My Contacts" group 
    activity.getContentResolver().insert(GroupMembership.CONTENT_URI,values);
    

    If you want to add to a specific user-defined group rather than My Contacts you will need to fish the right GROUP_ID out of the Contacts.Groups table.