Android通讯录之分组联系人

Android系统联系人信息通过 ContentProvider提供给我们使用,具体请参看contacts2.db

一、获取所有的分组信息

[java]  view plain  copy
  1. /** 
  2.      * 获取所有的 联系人分组信息 
  3.      *  
  4.      * @return 
  5.      */  
  6.     public List getAllGroupInfo() {  
  7.   
  8.         List groupList = new ArrayList();  
  9.   
  10.         Cursor cursor = null;  
  11.   
  12.         try {  
  13.             cursor = context.getContentResolver().query(Groups.CONTENT_URI,  
  14.                     nullnullnullnull);  
  15.   
  16.             while (cursor.moveToNext()) {  
  17.   
  18.                 GroupEntity ge = new GroupEntity();  
  19.   
  20.                 int groupId = cursor.getInt(cursor.getColumnIndex(Groups._ID)); // 组id  
  21.                 String groupName = cursor.getString(cursor  
  22.                         .getColumnIndex(Groups.TITLE)); // 组名  
  23.   
  24.                 ge.setGroupId(groupId);  
  25.                 ge.setGroupName(groupName);  
  26.   
  27.                 Log.i("MainActivity""group id:" + groupId + ">>groupName:"  
  28.                         + groupName);  
  29.   
  30.                 groupList.add(ge);  
  31.                 ge = null;  
  32.             }  
  33.   
  34.             return groupList;  
  35.   
  36.         } finally {  
  37.             if (cursor != null) {  
  38.                 cursor.close();  
  39.             }  
  40.         }  
  41.     }  


二、获取某个分组下的所有联系人信息

[java]  view plain  copy
  1. /** 
  2.      * 获取某个分组下的 所有联系人信息 
  3.      * 思路:通过组的id 去查询 RAW_CONTACT_ID, 通过RAW_CONTACT_ID去查询联系人 
  4.         要查询得到 data表的Data.RAW_CONTACT_ID字段 
  5.      * @param groupId 
  6.      * @return 
  7.      */  
  8.     public List getAllContactsByGroupId(int groupId) {  
  9.           
  10.         String[] RAW_PROJECTION = new String[] { ContactsContract.Data.RAW_CONTACT_ID, };  
  11.   
  12.         String RAW_CONTACTS_WHERE = ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID  
  13.                 + "=?"  
  14.                 + " and "  
  15.                 + ContactsContract.Data.MIMETYPE  
  16.                 + "="  
  17.                 + "'"  
  18.                 + ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE  
  19.                 + "'";  
  20.   
  21.         // 通过分组的id 查询得到RAW_CONTACT_ID  
  22.         Cursor cursor = context.getContentResolver().query(  
  23.                 ContactsContract.Data.CONTENT_URI, RAW_PROJECTION,  
  24.                 RAW_CONTACTS_WHERE, new String[] { groupId + "" }, "data1 asc");  
  25.   
  26.         List contactList = new ArrayList();  
  27.   
  28.         while (cursor.moveToNext()) {  
  29.             // RAW_CONTACT_ID  
  30.             int col = cursor.getColumnIndex("raw_contact_id");  
  31.             int raw_contact_id = cursor.getInt(col);  
  32.   
  33.             // Log.i("getAllContactsByGroupId", "raw_contact_id:" +  
  34.             // raw_contact_id);  
  35.   
  36.             ContactEntity ce = new ContactEntity();  
  37.   
  38.             ce.setContactId(raw_contact_id);  
  39.   
  40.             Uri dataUri = Uri.parse("content://com.android.contacts/data");  
  41.             Cursor dataCursor = context.getContentResolver().query(dataUri,  
  42.                     null"raw_contact_id=?",  
  43.                     new String[] { raw_contact_id + "" }, null);  
  44.   
  45.             while (dataCursor.moveToNext()) {  
  46.                 String data1 = dataCursor.getString(dataCursor  
  47.                         .getColumnIndex("data1"));  
  48.                 String mime = dataCursor.getString(dataCursor  
  49.                         .getColumnIndex("mimetype"));  
  50.   
  51.                 if ("vnd.android.cursor.item/phone_v2".equals(mime)) {  
  52.                     ce.setTelNumber(data1);  
  53.                 } else if ("vnd.android.cursor.item/name".equals(mime)) {  
  54.                     ce.setContactName(data1);  
  55.                 }  
  56.             }  
  57.   
  58.             dataCursor.close();  
  59.             contactList.add(ce);  
  60.             ce = null;  
  61.         }  
  62.   
  63.         cursor.close();  
  64.   
  65.         return contactList;  
  66.     }  

三、获取所有的联系人信息

[java]  view plain  copy
  1. /** 
  2.      * 获取手机所有联系人信息 
  3.      *  
  4.      * @return 
  5.      */  
  6.     public List getAllContacts() {  
  7.           
  8.         // 1.查询raw_contacts表 获取所有联系人的id  
  9.         Uri rawUri = Uri.parse("content://com.android.contacts/raw_contacts");  
  10.         Uri dataUri = Uri.parse("content://com.android.contacts/data");  
  11.           
  12.         List contaList = new ArrayList();  
  13.           
  14.         Cursor idCursor = context.getContentResolver().query(rawUri,  
  15.                 new String[] { "contact_id" }, nullnullnull);  
  16.           
  17.         while (idCursor.moveToNext()) {  
  18.               
  19.             int id = idCursor.getInt(0); // 得到联系人的id  
  20.               
  21.             ContactEntity ce = new ContactEntity();  
  22.             ce.setContactId(id);  
  23.               
  24.             Cursor dataCursor = context.getContentResolver().query(dataUri,  
  25.                     null"raw_contact_id=?"new String[] { id+"" }, null);  
  26.               
  27.             while (dataCursor.moveToNext()) {  
  28.                 String data1 = dataCursor.getString(dataCursor  
  29.                         .getColumnIndex("data1"));  
  30.                   
  31.                 String mime = dataCursor.getString(dataCursor  
  32.                         .getColumnIndex("mimetype"));  
  33.                 if ("vnd.android.cursor.item/phone_v2".equals(mime)) {  
  34.                     ce.setTelNumber(data1);  
  35.                 } else if ("vnd.android.cursor.item/name".equals(mime)) {  
  36.                     ce.setContactName(data1);  
  37.                 }  
  38.   
  39.             }  
  40.             dataCursor.close();  
  41.             contaList.add(ce);  
  42.             ce = null;  
  43.         }  
  44.           
  45.         idCursor.close();  
  46.           
  47.         return contaList;  
  48.     }  


以上代码 在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});   

你可能感兴趣的:(Android)