如何将联系人添加到组android [英] how to add a contact to a group android
问题描述
我有以下代码将联系人添加到组中,并将其添加到android的联系人应用/人脉应用中,它确实添加了该组,但没有添加该组中的联系人,我想念的是什么?我成功添加了联系人,同时也创建了组,我确实获得了这两个东西的ID,我使用以下代码将联系人与该组相关联,但它不起作用,组始终为空。
I have following code to add contact to a group into android's contact app / people app, it does add the group but not the contact in that group, what am i missing ? I am adding contact successfully also creating group, i do get the ids of both the things , i m using following code to associate the contact with the group but its not working , group is always empty.
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 this.getActivity().getContentResolver().insert(
ContactsContract.Data.CONTENT_URI, values);
}
****更新*****
我发现的另一件事是,我创建的这个群组与Google不同步,这可能就是未添加联系人的原因。
****update ***** Another thing i found is this group which i created doesn't get sync with google , probably thats the reason contacts aren't getting added.
推荐答案
最后可以将联系人添加到组中,这是必需的,创建一个与Google帐户同步的联系人(强制性),然后创建一个可以同步到默认同步服务的组,然后按照以下方式添加联系人在上面的代码中添加。
Finally could add a contact to group, this is what was required, create a contact that syncs with google account (mandatory), second create a group that can sync to default sync service and then add contact the way i am adding in above code.
如果您想知道如何创建可以同步的群组,这里就是
if you are curious in knowing how to create group that can sync, here it is
public String createGroup(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")));
return groupID;
}
这篇关于如何将联系人添加到组android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!