Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Want to create a new group in contacts programmatically

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);
like image 641
adithi Avatar asked May 26 '11 06:05

adithi


1 Answers

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

like image 79
adithi Avatar answered Oct 27 '22 20:10

adithi