0

嗨,我正在尝试创建新的 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);
4

2 回答 2

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);
    }
于 2017-03-16T04:42:23.333 回答
0

I see a lot of people posting examples where they do a subsequent query to get the id of the inserted element.

But the return from the insert is the Uri of the inserted row.

Why waste a call to the db?

long id = -1;
Uri insertedUri = getContentResolver().insert(...);
if (insertedUri!=null) {
   id = ContentUris.parseId(insertedUri);
}
于 2016-12-14T20:15:57.880 回答