I am working on android apps. I want to add a contact in android phone group. The code I am using is below:
ContentValues values = new ContentValues();
1)
public static void createNewGroup(String name)
{
try
{
ContentValues groupValues = new ContentValues();
groupValues.put(Groups.TITLE, name);
groupValues.put(Groups.SHOULD_SYNC, true);
groupValues.put(Groups.GROUP_VISIBLE, 1);
groupValues.putNull(Groups.ACCOUNT_TYPE);
groupValues.putNull(Groups.ACCOUNT_NAME);
getContentResolver().insert(Groups.CONTENT_URI, groupValues);
return true;
}
catch (Exception e){}
}
2)
public static Uri addContactToGroup(String rawContactId,String groupId)
{
try
{
ContentValues values = new ContentValues();
values.put(Data.RAW_CONTACT_ID, rawContactId);
values.put(GroupMembership.GROUP_ROW_ID, groupId);
values.put(Data.MIMETYPE, GroupMembership.CONTENT_ITEM_TYPE);
return getContentResolver().insert(Data.CONTENT_URI, values);
}
catch (Exception e)
{}
return Uri.EMPTY;
}
3)
public static Cursor getContactGroupId(String contactId)
{
Uri uri = ContactsContract.Data.CONTENT_URI;
String[] columns = new String[] { GroupMembership.GROUP_ROW_ID };
String where = Data.CONTACT_ID + " = ? AND " + Data.MIMETYPE + " = ?";
String[] args = new String[] {contactId, GroupMembership.CONTENT_ITEM_TYPE };
return Cursor contacts = getContentResolver().query(uri, columns, where, arg, null);
}
4)
public static Cursor getGroupsList(@Nullable String[] project,@Nullable String where,@Nullable String[] args,@Nullable String sort)
{
return getContentResolver().query(Groups.CONTENT_URI, project, where, args, sort);
}