Want to create a new group in contacts programmatically

I want to create a new contact group. I can request a group and display all group names, but I cannot create a group in android, which I tried to create a contact method but did not create ...

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);
+5
source share
3 answers

I found answer.i found in two ways, but I don't know what is the right or best way to use it. I shared this here.

its an easy way to add 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());
    }

}

thank

+13
source

groupValues.put(android.provider.Contacts.GroupMembership.GROUP_ID, 4); , , , .

0

Adithi answer Android 4.2.2, " " "", , , Android 4.4.6, " " "".

/ .

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());
    }
}
0

All Articles