Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to add a contact to a group android

I have following code to add contact to a group into android's contact app / people app, it does add the group but not the contact in that group, what am i missing ? I am adding contact successfully also creating group, i do get the ids of both the things , i m using following code to associate the contact with the group but its not working , 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 this group which i created doesn't get sync with google , probably thats the reason contacts aren't getting added.

like image 649
vishal dharankar Avatar asked Sep 01 '15 09:09

vishal dharankar


People also ask

Can I group Contacts on Android?

Open the Contacts app on your Android phone. At the top Right > tap settings > Contacts Manager> Contact Groups (refer to image 1).

How do I make a group in Contacts on my phone?

Google Android devices First, head to your Contacts app and see if there's an option labelled Groups or Create new Group. If so, follow the onscreen instructions to create your group, but if not, you'll have to oppen the messaging app and then select all the different contacts you want to send the message to.


2 Answers

Finally could add a contact to group, this is what was required, create a contact that syncs with google account (mandatory), second create a group that can sync to default sync service and then add contact the way i am adding in above code.

if you are curious in knowing how to create 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;


}
like image 166
vishal dharankar Avatar answered Nov 06 '22 15:11

vishal dharankar


Use ContentProviderOperation for this.

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

If the group is existing 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 group is not existing:

// 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);
like image 34
La Machine Avatar answered Nov 06 '22 14:11

La Machine