想要以编程方式在联系人中创建新组

adi*_*thi 5 android android-contacts

我想创建一个新的联系人组.我可以查询该组并显示所有组名但我不能在android中创建一个组我尝试创建联系人方法但没有创建...

ContentResolver cr = this.getContentResolver();
    groupValues = new ContentValues();
    Log.e("Group","start");
    groupValues.put(android.provider.Contacts.GroupMembership.GROUP_ID, 4);
    groupValues.put(android.provider.Contacts.GroupMembership.NAME, "Sriseshaa");
    groupValues.put(android.provider.Contacts.GroupMembership.PERSON_ID, 1);

    cr.insert(android.provider.Contacts.GroupMembership.CONTENT_URI, groupValues);
Run Code Online (Sandbox Code Playgroud)

adi*_*thi 13

我找到了答案.我发现有两种方式,但我不知道哪种是正确的或最好的方式使用.我在这里分享这些..

它的简单方式,如添加联系人,

ContentValues groupValues;
create group()
{
 ContentResolver cr = this.getContentResolver();
 groupValues = new ContentValues();
 groupValues.put(ContactsContract.Groups.TITLE, "MyContactGroup");
 cr.insert(ContactsContract.Groups.CONTENT_URI, groupValues);
}
Run Code Online (Sandbox Code Playgroud)

另一种使用ContentProviderOperation的方法

 private void createGroup() {
    ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();

    ops.add(ContentProviderOperation
            .newInsert(ContactsContract.Groups.CONTENT_URI)
            .withValue(ContactsContract.Groups.TITLE, "SRI").build());
    try {

        getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);

    } catch (Exception e) {
        Log.e("Error", e.toString());
    }

}
Run Code Online (Sandbox Code Playgroud)

谢谢