正文  设备功能 > 联系人/Contacts >

Android Contacts的使用(二)

Granting Access 授予权限 <uses-permission android:name="android.permission.READ_CONTACTS" /> Qu...

Granting Access 授予权限
<uses-permission android:name="android.permission.READ_CONTACTS" />

Querying the contact database 联系人数据库查询


Retrieving Contact Details 获取联系方式

本的联系人信息存储在联系人表中,而详细信息存储在个人表中。在 Android1.x 中查询的联系人记录数据库的URI是People.CONTENT_URI。

Java代码 
package com.test;import android.app.Activity; 
import android.content.ContentResolver; 
import android.database.Cursor;import android.os.Bundle; 
import android.provider.Contacts.People; 
public class TestContacts extends Activity {     
@Override    public void onCreate(Bundle savedInstanceState) {  
       super.onCreate(savedInstanceState);         
       setContentView(R.layout.main);        
       ContentResolver cr = getContentResolver();         
       Cursor cur = cr.query(People.CONTENT_URI, null, null, null, null);         
        if(cur.getCount()>0){            
          while (cur.moveToNext()) {                 
             String id = cur.getString(cur.getColumnIndex(People._ID));                 
             String name = cur.getString(cur.getColumnIndex(People.DISPLAY_NAME));             
          }         
}          


Phone Numbers 电话号码
查询的URI换成Contacts.Phones.CONTENT_URI。
Java代码 
if (cur.getInt(cur.getColumnIndex(People.PRIMARY_PHONE_ID)) > 0) {                     
        Cursor pCur = cr.query(Contacts.Phones.CONTENT_URI,null,                            Contacts.Phones.PERSON_ID +" = ?",new String[]{id}, null); 
         int i=0;                     
         int pCount = pCur.getCount();                     
         String[] phoneNum = new String[pCount];                      String[] phoneType = new String[pCount];                     
 while (pCur.moveToNext()) {    
          phoneNum[i] = pCur.getString(pCur.getColumnIndex(Contacts.Phones.NUMBER));          
           phoneType[i] = pCur.getString(pCur.getColumnIndex(Contacts.Phones.TYPE));                                                         i++;                     
}     

以存储多个电话号码,我们需要遍历返回的结果。查询除了返回电话号码外还返回了其他(家庭,工作,手机等)类型。

Email Addresses 邮件地址  
Java代码 
Cursor emailCur = cr.query(Contacts.ContactMethods.CONTENT_EMAIL_URI,null,                        Contacts.ContactMethods.PERSON_ID + " = ?",new String[]{id}, null);                 
   while (emailCur.moveToNext()) {                     
 // This would allow you get several email addresses                }                  
    emailCur.close();   
   

Notes 注释  
为每个联系人附加自定义注释。注释存储在联系人记录中,并通过People.NOTES存储的数据进行访问。

String notes=cur.getString(cur.getColumnIndex(People.NOTES));

Postal Addresses 邮政地址
每个联系人都可以存储多个邮政地址。地址存储在联系方式表中,要获取数据,需要有次要条件。添加适配Contacts.ContactMethods.CONTENT_POSTAL_ITEM_TYPE的条件Contacts.ContactMethods.KIND来访问Contacts.ContactMethods.CONTENT_URI传递过来的地址。

Java代码 
String addrWhere = Contacts.ContactMethods.PERSON_ID                 + " = ? AND " + Contacts.ContactMethods.KIND + " = ?";                 
 String[] addrWhereParams = new String[]{id,                     Contacts.ContactMethods.CONTENT_POSTAL_ITEM_TYPE};                         Cursor addrCur = cr.query(Contacts.ContactMethods.CONTENT_URI,                             null, addrWhere, addrWhereParams, null);                 while(addrCur.moveToNext()) {                    String addr = addrCur.getString(                               addrCur.getColumnIndex(Contacts.ContactMethodsColumns.DATA));                    String type = addrCur.getString(                               addrCur.getColumnIndex(Contacts.ContactMethodsColumns.TYPE));                }                 addrCur.close();