5

I want to create a new contact group. I can query the group and display all the group names but I can't create a group in android I tried as creating contacts method but not created...

ContentResolver cr = this.getContentResolver();
    groupValues = new ContentValues();
    Log.e("Group","start");
    groupValues.put(android.provider.Contacts.GroupMembership.GROUP_ID, 4);
    groupValues.put(android.provider.Contacts.GroupMembership.NAME, "Sriseshaa");
    groupValues.put(android.provider.Contacts.GroupMembership.PERSON_ID, 1);

    cr.insert(android.provider.Contacts.GroupMembership.CONTENT_URI, groupValues);
Sathyajith Bhat
  • 21,321
  • 22
  • 95
  • 134
adithi
  • 973
  • 3
  • 11
  • 19

3 Answers3

14

i found the answer.i found in two ways but i dont know which is correct or best way to use.i am sharing those here..

its simple way like adding contact,

ContentValues groupValues;
create group()
{
 ContentResolver cr = this.getContentResolver();
 groupValues = new ContentValues();
 groupValues.put(ContactsContract.Groups.TITLE, "MyContactGroup");
 cr.insert(ContactsContract.Groups.CONTENT_URI, groupValues);
}

Another method using ContentProviderOperation

 private void createGroup() {
    ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();

    ops.add(ContentProviderOperation
            .newInsert(ContactsContract.Groups.CONTENT_URI)
            .withValue(ContactsContract.Groups.TITLE, "SRI").build());
    try {

        getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);

    } catch (Exception e) {
        Log.e("Error", e.toString());
    }

}

Thanks

adithi
  • 973
  • 3
  • 11
  • 19
  • BY default 3 options are already there in groups 1.Friends 2. family , 3. CoWorkes. now my question is how to add contact to a specific group in those there options. – Anil Chandra Varma Dandu Jan 06 '16 at 11:15
1

adithi's answer is enough for Android 4.2.2, in which the name of Contacts manager application is "Contacts" , but the group created by that code will not show on Android 4.4,6 in which the name of Contacts manager application is "People".

The group would show up after adding the account type/name information while insertion happens.

private void createGroup() {

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

        ops.add(ContentProviderOperation
                .newInsert(ContactsContract.Groups.CONTENT_URI)
                .withValue(
                        ContactsContract.Groups.TITLE,
                        Constants.CC_CONTACT_GROUP_TITLE)
                .withValue(
                        ContactsContract.Groups.ACCOUNT_TYPE,
                        Constants.CC_CONTACT_GROUP_ACCOUNT_TYPE)
                .withValue(
                        ContactsContract.Groups.ACCOUNT_NAME,
                        Constants.CC_CONTACT_GROUP_ACCOUNT_NAME)
                .build());
    try {

        getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);

    } catch (Exception e) {
        Log.e("Error", e.toString());
    }
}
Zephyr
  • 6,123
  • 34
  • 33
0

Why are you specifying group ID with groupValues.put(android.provider.Contacts.GroupMembership.GROUP_ID, 4); Its androids job to determined the group ID, you cant specify it because you don't know whether this id is already taken or not.

Sazzad Hissain Khan
  • 37,929
  • 33
  • 189
  • 256