Android系统联系人信息通过 ContentProvider提供给我们使用,具体请参看contacts2.db
一、获取所有的分组信息
public List<GroupEntity> getAllGroupInfo() { List<GroupEntity> groupList = new ArrayList<GroupEntity>(); Cursor cursor = null ; try { cursor = context.getContentResolver().query(Groups.CONTENT_URI, null , null , null , null ); while (cursor.moveToNext()) { GroupEntity ge = new GroupEntity(); int groupId = cursor.getInt(cursor.getColumnIndex(Groups._ID)); String groupName = cursor.getString(cursor .getColumnIndex(Groups.TITLE)); ge.setGroupId(groupId); ge.setGroupName(groupName); Log.i("MainActivity" , "group id:" + groupId + ">>groupName:" + groupName); groupList.add(ge); ge = null ; } return groupList; } finally { if (cursor != null ) { cursor.close(); } } }
二、获取某个分组下的所有联系人信息
public List<ContactEntity> getAllContactsByGroupId( int groupId) { String[] RAW_PROJECTION = new String[] { ContactsContract.Data.RAW_CONTACT_ID, }; String RAW_CONTACTS_WHERE = ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID + "=?" + " and " + ContactsContract.Data.MIMETYPE + "=" + "'" + ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE + "'" ; Cursor cursor = context.getContentResolver().query( ContactsContract.Data.CONTENT_URI, RAW_PROJECTION, RAW_CONTACTS_WHERE, new String[] { groupId + "" }, "data1 asc" ); List<ContactEntity> contactList = new ArrayList<ContactEntity>(); while (cursor.moveToNext()) { int col = cursor.getColumnIndex( "raw_contact_id" ); int raw_contact_id = cursor.getInt(col); ContactEntity ce = new ContactEntity(); ce.setContactId(raw_contact_id); Uri dataUri = Uri.parse("content://com.android.contacts/data" ); Cursor dataCursor = context.getContentResolver().query(dataUri, null , "raw_contact_id=?" , new String[] { raw_contact_id + "" }, null ); while (dataCursor.moveToNext()) { String data1 = dataCursor.getString(dataCursor .getColumnIndex("data1" )); String mime = dataCursor.getString(dataCursor .getColumnIndex("mimetype" )); if ( "vnd.android.cursor.item/phone_v2" .equals(mime)) { ce.setTelNumber(data1); } else if ( "vnd.android.cursor.item/name" .equals(mime)) { ce.setContactName(data1); } } dataCursor.close(); contactList.add(ce); ce = null ; } cursor.close(); return contactList; }
三、获取所有的联系人信息
public List<ContactEntity> getAllContacts() { Uri rawUri = Uri.parse("content://com.android.contacts/raw_contacts" ); Uri dataUri = Uri.parse("content://com.android.contacts/data" ); List<ContactEntity> contaList = new ArrayList<ContactEntity>(); Cursor idCursor = context.getContentResolver().query(rawUri, new String[] { "contact_id" }, null , null , null ); while (idCursor.moveToNext()) { int id = idCursor.getInt( 0 ); ContactEntity ce = new ContactEntity(); ce.setContactId(id); Cursor dataCursor = context.getContentResolver().query(dataUri, null , "raw_contact_id=?" , new String[] { id+ "" }, null ); while (dataCursor.moveToNext()) { String data1 = dataCursor.getString(dataCursor .getColumnIndex("data1" )); String mime = dataCursor.getString(dataCursor .getColumnIndex("mimetype" )); if ( "vnd.android.cursor.item/phone_v2" .equals(mime)) { ce.setTelNumber(data1); } else if ( "vnd.android.cursor.item/name" .equals(mime)) { ce.setContactName(data1); } } dataCursor.close(); contaList.add(ce); ce = null ; } idCursor.close(); return contaList; }
以上代码 在Android 4.0.3上测试没有问题
注:新建、删除群组等操作如下:
新建组(名字为name): ContentValues values = new ContentValues(); values.put(Groups.TITLE, name); getContentResolver().inser(Groups.CONTENT_URI, values); 删除组(Id为groupId): getContentResolver().delete(Uri.parse(Groups.CONTENT_URI +"?" +ContactsContract.CALLER_IS_SYNCADAPTER + "=true"),Groups._ID+"="+groupId,null); 给组重命名(oldName;newName;groupId): Uri uri = ContentUris.withAppendedId(Groups.CONTENT_URI, groupId); ContentValues values = new ContentValues(); values.put(Groups.TITLE,newName); getContentResolver().update(uri,values,null,null); 给组添加成员(groupId,personId): 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): 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});