ContactsContract获取联系人信息

来源:互联网 发布:网络用语whf什么意思 编辑:程序博客网 时间:2024/05/17 23:22

一、 从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.     }  

来源:http://www.cnblogs.com/ruiyi1987/archive/2011/06/20/2084925.html

 

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.     }  

来源:http://www.apkbus.com/android-14565-1-1.html

0 0
原创粉丝点击