您好我正在尝试创建新的gmail组并添加联系人。我成功创建了一个组但联系人没有被添加到它。我已经在stackoverflow中读了很多答案但没有任何效果。我无法想象哪里出错了。我在这里发布我的代码请帮助。
创建群组
public String createGroupInPhone() {
String[] GROUP_PROJECTION = new String[]{ContactsContract.Groups._ID, ContactsContract.Groups.TITLE};
ContentValues contentValues = null;
try {
ContentResolver cr = this.getContentResolver();
contentValues = new ContentValues();
contentValues.put(ContactsContract.Groups.TITLE, groupName);
contentValues.put(ContactsContract.Groups.SHOULD_SYNC, true);
contentValues.put(ContactsContract.Groups.GROUP_VISIBLE, 1);
contentValues.put(ContactsContract.Groups.ACCOUNT_TYPE, "com.google");
contentValues.put(ContactsContract.Groups.ACCOUNT_NAME, "v.satya.rc@gmail.com");
cr.insert(ContactsContract.Groups.CONTENT_URI, contentValues);
} catch (Exception e) {
e.printStackTrace();
}
String groupID;
Cursor getGroupID_Cursor;
getGroupID_Cursor = this.getContentResolver().query(ContactsContract.Groups.CONTENT_URI, GROUP_PROJECTION, ContactsContract.Groups.TITLE + "=?", new String[]{groupName}, null);
getGroupID_Cursor.moveToFirst();
groupID = (getGroupID_Cursor.getString(getGroupID_Cursor.getColumnIndex(ContactsContract.Groups._ID)));
String groupTitle = (getGroupID_Cursor.getString(getGroupID_Cursor.getColumnIndex(ContactsContract.Groups.TITLE)));
System.out.println("Group Title: " + groupTitle);
getGroupID_Cursor.close();
return groupID;
}
我有疑问。这个ContactsContract.Groups._ID如何与GROUP_ROW_ID,GROUP_SOURCE_ID相关
将联系人添加到新群组
public void addContactsToPhoneGroups(String contact_id, String groupId, String groupName) {
System.out.println("ContactId: " + contact_id);
System.out.println("GroupId: " + groupId);
ArrayList<ContentProviderOperation> ops = new ArrayList<>();
ContentValues values = new ContentValues();}
values.put(ContactsContract.CommonDataKinds.GroupMembership.RAW_CONTACT_ID,
contact_id);
values.put(
ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID,
groupId);
values
.put(
ContactsContract.CommonDataKinds.GroupMembership.MIMETYPE,
ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE);
getContentResolver().insert(
ContactsContract.Data.CONTENT_URI, values);
答案 0 :(得分:1)
1)
public static void createNewGroup(String name,@Nullable String accountName, @Nullable String accountType)
{
try
{
ContentValues groupValues = new ContentValues();
groupValues.put(Groups.TITLE, name);
groupValues.put(Groups.GROUP_VISIBLE, 1);
//groupValues.put(Settings.UNGROUPED_VISIBLE, true);
if(accountType != null && accountName != null)
{
groupValues.put(Groups.ACCOUNT_TYPE, accountType);
groupValues.put(Groups.ACCOUNT_NAME, accountName);
groupValues.put(Groups.SHOULD_SYNC, true);
}
else
{
groupValues.putNull(Groups.ACCOUNT_TYPE);
groupValues.putNull(Groups.ACCOUNT_NAME);
groupValues.put(Groups.SHOULD_SYNC, false);
}
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);
}
答案 1 :(得分:0)
我看到很多人发布示例,他们在后续查询中获取插入元素的id。
但是插入的返回是插入行的Uri。
为什么要浪费对db的调用?
long id = -1;
Uri insertedUri = getContentResolver().insert(...);
if (insertedUri!=null) {
id = ContentUris.parseId(insertedUri);
}