我正在尝试使用以下代码创建一个组和该组的联系人。 但是使用此代码可以创建新的组/标签,但联系人不会添加到该组。 请建议。
public String createGroupLatest(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")));
Log.v("TAG","==groupID=="+groupID);
return groupID;
}
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 getContentResolver().insert(ContactsContract.Data.CONTENT_URI, values);
}