ContactsContract获取联系人信息

转载http://uule.iteye.com/blog/1709227


一、 从Android 2.0 SDK开始有关联系人provider的类变成了ContactsContract,虽然老的android.provider.Contacts能用,但是在SDK中标记为为deprecated将被放弃不推荐的方法,而从Android 2.0及API Level为5开始新增了android.provider.ContactsContract来代替原来的方法。
ContactsContract的子类ContactsContract.Contacts是一张表,代表了所有联系人的统计信息。比如联系人ID(—ID),查询键(LOOKUP_KEY),联系人的姓名(DISPLAY_NAME_PRIMARY),头像的id(PHOTO_ID)以及群组的id等等。

我们可以通过以下的方法取得所有联系人的表的Cursor对象:
1)ContentResolver contentResolver=getContentResolver();//获取 ContentResolver对象查询在ContentProvider里定义的共享对象;

2)Cursor cursor=contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
   //根据URI对象ContactsContract.Contacts.CONTENT_URI查询所有联系人;

从Cursor对象里我们关键是要取得联系人的_id。通过它,再通过ContactsContract.CommonDataKinds的各个子类查询该_id联系人的电话(ContactsContract.CommonDataKinds.Phone),email(ContactsContract.CommonDataKinds.Email)等等。
以取得该联系人所有电话为例:
1)int idFieldIndex=cursor.getColumnIndex(ContactsContract.Contacts._ID);
   int id=cursor.getInt(idFieldIndex);//根据列名取得该联系人的id;
2)Cursor phonecursor=contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID+"=?", new String[]{Integer.toString(id)}, null);
 //再类ContactsContract.CommonDataKinds.Phone中根据查询相应id联系人的所有电话;
类似地可以ContactsContract.CommonDataKinds的不同的子类查询不同的内容。android文档告诉我们推荐使用ContactsContract.Contacts.LOOKUP_KEY代替ContactsContract.Contacts._ID。

最后,由于读取联系人比较的占用资源,为了提高用户的体验度。考虑将读取的过程放在线程里完成,推荐使用AsyncTask类。

二、

在2.1中,display_name 在contact表中,而data表中包含很多contact的数据,比如电话,姓名,email等 ,如果要查询一个联系人的姓名和电话,网上大多数的做法是:
先查contact表,得到姓名,再根据has_phone_number是否是1决定data表中有无电话记录。这样是要查发2个uri,得到两个cursor在分别去除需要的内容。
其实可以直接发一个uri就查处姓名和号码,代码:
Cursor c=getContentResolver().query(ContactsContract.Data.CONTENT_URI, null, "mimetype='vnd.android.cursor.item/phone_v2'", null, null);
这个uri对象data,他会去查view_data这个试图,试图定义就是data,raw_contacts,mimetypes,group等一系列表的联合查询,而上面这个uri发出去会得到很多字段,当条件为mimetype=vnd.android.cursor.item/phone_v2则查的是此人电话所对应的那条记录,其中也会得到姓名,
注意:
姓名是根据display_name 取得
电话是根据data1取得

 

Example1:

Java代码   收藏代码
  1. public void onCreate(Bundle savedInstanceState) {  
  2.         super.onCreate(savedInstanceState);  
  3.           
  4.         Uri contactsUri=ContactsContract.Contacts.CONTENT_URI;  
  5.         String[] proj1=new String[]{ContactsContract.Contacts.DISPLAY_NAME,  
  6.                                     ContactsContract.Contacts.HAS_PHONE_NUMBER,  
  7.                                     ContactsContract.Contacts.LOOKUP_KEY};  
  8.         Cursor curContacts=getContentResolver().query(contactsUri,proj1, nullnullnull);  
  9.              
  10.          ArrayList<String> contactsList=new ArrayList<String>();   
  11.         String allPhoneNo="";  
  12.         if(curContacts.getCount()>0){  
  13.             while(curContacts.moveToNext()){     
  14.                 // get all the phone numbers if exist  
  15.                 if(curContacts.getInt(1)>0){  
  16.                     allPhoneNo=getAllPhoneNumbers(curContacts.getString(2));  
  17.                 }  
  18.                 contactsList.add(curContacts.getString(0)+" , "+allPhoneNo);  
  19.                 allPhoneNo="";  
  20.             }  
  21.         }  
  22.      
  23.         // binding the data to ListView   
  24.         setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, contactsList));  
  25.         ListView lv=getListView();  
  26.         lv.setTextFilterEnabled(true);  
  27.           
  28.     }  
  29.       
  30.     /** 
  31.      * Get all the phone numbers of a specific contact person     
  32.      */  
  33.     public String getAllPhoneNumbers(String lookUp_Key){  
  34.         String allPhoneNo="";  
  35.           
  36.         // Phone info are stored in the ContactsContract.Data table   
  37.         Uri phoneUri=ContactsContract.CommonDataKinds.Phone.CONTENT_URI;  
  38.         String[] proj2={ContactsContract.CommonDataKinds.Phone.NUMBER};  
  39.         // using lookUp key to search the phone numbers  
  40.         String selection=ContactsContract.Data.LOOKUP_KEY+"=?";  
  41.   
  42.         Cursor cur=getContentResolver().query(phoneUri,proj2,selection, new String[]{lookUp_Key}, null);  
  43.         while(cur.moveToNext()){  
  44.             allPhoneNo+=cur.getString(0)+" ";  
  45.         }  
  46.   
  47.         return allPhoneNo;          
  48.     }  

 

Example2:

Java代码   收藏代码
  1. private List<HashMap<String, String>> fillMaps() {  
  2.         List<HashMap<String, String>> items = new ArrayList<HashMap<String, String>>();  
  3.   
  4.         Cursor cur = null;  
  5.         try {  
  6.             // Query using ContentResolver.query or Activity.managedQuery  
  7.             cur = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, nullnullnullnull);  
  8.             if (cur.moveToFirst()) {  
  9.                 int idColumn = cur.getColumnIndex(ContactsContract.Contacts._ID);  
  10.                 int displayNameColumn = cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);  
  11.                 // Iterate all users  
  12.                 do {  
  13.                     String phoneNumber = "";  
  14.                     String contactId = cur.getString(idColumn);  
  15.                     String displayName = cur.getString(displayNameColumn);  
  16.                     // Get number of user's phoneNumbers  
  17.                     int numberCount = cur.getInt(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));  
  18.                     if (numberCount > 0) {  
  19.                         Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId  
  20.                         /* 
  21.                          * + " and " + ContactsContract.CommonDataKinds 
  22.                          * .Phone.TYPE + "=" + ContactsContract.CommonDataKinds 
  23.                          * .Phone.TYPE_MOBILE 
  24.                          */nullnull);  
  25.                         if (phones.moveToFirst()) {  
  26.                             int numberColumn = phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);  
  27.                             do {  
  28.                                 phoneNumber += phones.getString(numberColumn) + ",";  
  29.                             } while (phones.moveToNext());  
  30.                         }  
  31.                     }  
  32.                     // Add values to items  
  33.                     HashMap<String, String> i = new HashMap<String, String>();  
  34.                     i.put("name", displayName);  
  35.                     i.put("key", phoneNumber);  
  36.                     items.add(i);  
  37.                 } while (cur.moveToNext());  
  38.             } else {  
  39.                 HashMap<String, String> i = new HashMap<String, String>();  
  40.                 i.put("name""Your Phone");  
  41.                 i.put("key""Have No Contacts.");  
  42.                 items.add(i);  
  43.             }  
  44.         } finally {  
  45.             if (cur != null)  
  46.                 cur.close();  
  47.         }  
  48.         return items;  
  49.     }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值