Question

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.

Was it helpful?

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.

OTHER TIPS

To make it work with the new ContactsContract API, you can add this to the ContentProviderOperation list:

ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
            .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
            .withValue(ContactsContract.Data.MIMETYPE,
                    ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE)
            .withValue(ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID, 1)
            .build());
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top