对android联系人分组的增删改查
// 默认查询全部的分组这个请参看 点击打开链接
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
package com.xzq.test2; import android.app.Activity; import android.content.ContentUris; import android.content.ContentValues; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.provider.ContactsContract; import android.provider.ContactsContract.Groups; public class GroupAty extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 默认查询全部的分组 Cursor cursor = getContentResolver().query(Groups.CONTENT_URI, null, null, null, null); while (cursor.moveToNext()) { String name = cursor.getString(cursor.getColumnIndex(Groups.TITLE)); long id = cursor.getLong(cursor.getColumnIndex(Groups._ID)); } // 新建组 String name = ""; ContentValues valuess = new ContentValues(); valuess.put(Groups.TITLE, name); getContentResolver().insert(Groups.CONTENT_URI, valuess); // 删除组(Id为groupId): String gid = ""; getContentResolver().delete( Uri.parse(Groups.CONTENT_URI + "?" + ContactsContract.CALLER_IS_SYNCADAPTER + "=true"), Groups._ID + "=" + gid, null); // 给组重命名(oldName;newName;groupId): long groupID = 0; String newName = ""; Uri uri = ContentUris.withAppendedId(Groups.CONTENT_URI, groupID); ContentValues value = new ContentValues(); value.put(Groups.TITLE, newName); getContentResolver().update(uri, value, null, null); // 给组添加成员(groupId,personId): String personid = "";// 联系人ID long groupid = 0;// 分组ID 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); getContentResolver().insert(ContactsContract.Data.CONTENT_URI, values); // 给组移除成员(groupId,personId): String personId = "";// 联系人ID String groupId = "";// 分组ID getContentResolver().delete( ContactsContract.Data.CONTENT_URI, ContactsContract.CommonDataKinds.GroupMembership.RAW_CONTACT_ID + "=? and " + ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID + "=? and " + ContactsContract.CommonDataKinds.GroupMembership.MIMETYPE + "=?", new String[] { personId, groupId, ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE }); } }