首页 > 代码库 > Android Contacts Provider

Android Contacts Provider

public interface BaseColumns
 {
  public static final String _ID = "_id";
  public static final String _COUNT = "_count";
 }

  protected interface ContactsColumns
  {
        public static final String DISPLAY_NAME = ContactNameColumns.DISPLAY_NAME_PRIMARY;  //通讯录中人名
     public static final String PHOTO_ID = "photo_id";
     public static final String PHOTO_FILE_ID = "photo_file_id";
     public static final String PHOTO_URI = "photo_uri";
  public static final String PHOTO_THUMBNAIL_URI = "photo_thumb_uri";
     public static final String IN_VISIBLE_GROUP = "in_visible_group";
  public static final String IS_USER_PROFILE = "is_user_profile";
  public static final String HAS_PHONE_NUMBER = "has_phone_number";
  public static final String LOOKUP_KEY = "lookup";
  public static final String CONTACT_LAST_UPDATED_TIMESTAMP ="contact_last_updated_timestamp";
 
  }
   protected interface ContactOptionsColumns
   {
        public static final String TIMES_CONTACTED = "times_contacted";
  public static final String LAST_TIME_CONTACTED = "last_time_contacted";
  public static final String STARRED = "starred";
  public static final String CUSTOM_RINGTONE = "custom_ringtone";
  public static final String SEND_TO_VOICEMAIL = "send_to_voicemail";
   }
    protected interface ContactNameColumns
 {
     public static final String DISPLAY_NAME_SOURCE = "display_name_source";
  public static final String DISPLAY_NAME_PRIMARY = "display_name";
  public static final String DISPLAY_NAME_ALTERNATIVE = "display_name_alt";
  public static final String PHONETIC_NAME_STYLE = "phonetic_name_style";
  public static final String PHONETIC_NAME = "phonetic_name";
  public static final String SORT_KEY_PRIMARY = "sort_key";
  public static final String SORT_KEY_ALTERNATIVE = "sort_key_alt";
 }
  protected interface ContactStatusColumns
  {
     public static final String CONTACT_PRESENCE = "contact_presence";
  public static final String CONTACT_CHAT_CAPABILITY = "contact_chat_capability";
  public static final String CONTACT_STATUS = "contact_status";
  public static final String CONTACT_STATUS_TIMESTAMP = "contact_status_ts";
     public static final String CONTACT_STATUS_RES_PACKAGE = "contact_status_res_package";
     public static final String CONTACT_STATUS_LABEL = "contact_status_label";
     public static final String CONTACT_STATUS_ICON = "contact_status_icon";
  }

 

 

遍历通讯录中所有的列名及对应的值:

public class MainActivity extends Activity {

 class People
 {
  private String colName;
  private String colVal;
  public String getColName() {
   return colName;
  }
  public void setColName(String colName) {
   this.colName = colName;
  }
  public String getColVal() {
   return colVal;
  }
  public void setColVal(String colVal) {
   this.colVal = colVal;
  }
 }
 private List<People> peoples=new ArrayList<>();
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  ContentResolver cr=getContentResolver();
  Cursor cur=cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null,null);
  while(cur.moveToNext()){        
             String[] columnNames = cur.getColumnNames();
             for (int i = 0; i < columnNames.length; i++) {
           People p=new People();
           p.setColName(columnNames[i]);
           p.setColVal(cur.getString(cur.getColumnIndex(columnNames[i])));
           peoples.add(p);
             }
     }
  
  for (int i = 0; i < peoples.size(); i++) {
   System.out.println("列明:"+ peoples.get(i).getColName()+"  列值:"+peoples.get(i).getColVal());
   
  }

 

 

效果如下:

11-06 10:46:54.147: I/System.out(25193): 列明:send_to_voicemail  列值:0
11-06 10:46:54.147: I/System.out(25193): 列明:organization_note  列值:null
11-06 10:46:54.147: I/System.out(25193): 列明:custom_ringtone  列值:null
11-06 10:46:54.147: I/System.out(25193): 列明:name_raw_contact_id  列值:15
11-06 10:46:54.147: I/System.out(25193): 列明:photo_file_id  列值:null
11-06 10:46:54.147: I/System.out(25193): 列明:has_phone_number  列值:1
11-06 10:46:54.147: I/System.out(25193): 列明:contact_status_label  列值:null
11-06 10:46:54.147: I/System.out(25193): 列明:display_name  列值:李唐(妈)
11-06 10:46:54.147: I/System.out(25193): 列明:sort_key_alt  列值:LI 李 TANG 唐 ( MA 妈 )
11-06 10:46:54.147: I/System.out(25193): 列明:in_visible_group  列值:1
11-06 10:46:54.147: I/System.out(25193): 列明:starred  列值:0
11-06 10:46:54.147: I/System.out(25193): 列明:display_name_alt  列值:李唐(妈)
11-06 10:46:54.147: I/System.out(25193): 列明:sort_key  列值:LI 李 TANG 唐 ( MA 妈 )
11-06 10:46:54.147: I/System.out(25193): 列明:contact_presence  列值:null
11-06 10:46:54.147: I/System.out(25193): 列明:contact_status_res_package  列值:null
11-06 10:46:54.147: I/System.out(25193): 列明:contact_status_ts  列值:null
11-06 10:46:54.147: I/System.out(25193): 列明:sns_type  列值:0
11-06 10:46:54.147: I/System.out(25193): 列明:times_contacted  列值:64
11-06 10:46:54.147: I/System.out(25193): 列明:contact_status  列值:null
11-06 10:46:54.147: I/System.out(25193): 列明:phonetic_name  列值:null
11-06 10:46:54.147: I/System.out(25193): 列明:phone_number  列值:15936165107
11-06 10:46:54.147: I/System.out(25193): 列明:phonetic_name_style  列值:0
11-06 10:46:54.152: I/System.out(25193): 列明:is_user_profile  列值:0
11-06 10:46:54.152: I/System.out(25193): 列明:lookup  列值:930iflyme-17
11-06 10:46:54.152: I/System.out(25193): 列明:contact_status_icon  列值:null
11-06 10:46:54.152: I/System.out(25193): 列明:last_time_contacted  列值:1403269106387
11-06 10:46:54.152: I/System.out(25193): 列明:_id  列值:16
11-06 10:46:54.152: I/System.out(25193): 列明:display_name_source  列值:40
11-06 10:46:54.152: I/System.out(25193): 列明:photo_uri  列值:null
11-06 10:46:54.152: I/System.out(25193): 列明:photo_thumb_uri  列值:null
11-06 10:46:54.152: I/System.out(25193): 列明:contact_chat_capability  列值:null
11-06 10:46:54.152: I/System.out(25193): 列明:photo_id  列值:null

 


11-06 10:46:54.152: I/System.out(25193): 列明:send_to_voicemail  列值:0
11-06 10:46:54.152: I/System.out(25193): 列明:organization_note  列值:null
11-06 10:46:54.152: I/System.out(25193): 列明:custom_ringtone  列值:null
11-06 10:46:54.152: I/System.out(25193): 列明:name_raw_contact_id  列值:17
11-06 10:46:54.152: I/System.out(25193): 列明:photo_file_id  列值:null
11-06 10:46:54.152: I/System.out(25193): 列明:has_phone_number  列值:1
11-06 10:46:54.152: I/System.out(25193): 列明:contact_status_label  列值:null
11-06 10:46:54.152: I/System.out(25193): 列明:display_name  列值:刘凯
11-06 10:46:54.152: I/System.out(25193): 列明:sort_key_alt  列值:LIU 刘 KAI 凯
11-06 10:46:54.152: I/System.out(25193): 列明:in_visible_group  列值:1
11-06 10:46:54.152: I/System.out(25193): 列明:starred  列值:0
11-06 10:46:54.152: I/System.out(25193): 列明:display_name_alt  列值:刘凯
11-06 10:46:54.152: I/System.out(25193): 列明:sort_key  列值:LIU 刘 KAI 凯
11-06 10:46:54.152: I/System.out(25193): 列明:contact_presence  列值:null
11-06 10:46:54.152: I/System.out(25193): 列明:contact_status_res_package  列值:null
11-06 10:46:54.152: I/System.out(25193): 列明:contact_status_ts  列值:null
11-06 10:46:54.152: I/System.out(25193): 列明:sns_type  列值:0
11-06 10:46:54.152: I/System.out(25193): 列明:times_contacted  列值:40
11-06 10:46:54.152: I/System.out(25193): 列明:contact_status  列值:null
11-06 10:46:54.152: I/System.out(25193): 列明:phonetic_name  列值:null
11-06 10:46:54.157: I/System.out(25193): 列明:phone_number  列值:18736579852
11-06 10:46:54.157: I/System.out(25193): 列明:phonetic_name_style  列值:0
11-06 10:46:54.157: I/System.out(25193): 列明:is_user_profile  列值:0
11-06 10:46:54.157: I/System.out(25193): 列明:lookup  列值:930iflyme-16
11-06 10:46:54.157: I/System.out(25193): 列明:contact_status_icon  列值:null
11-06 10:46:54.157: I/System.out(25193): 列明:last_time_contacted  列值:1414759606574
11-06 10:46:54.157: I/System.out(25193): 列明:_id  列值:17
11-06 10:46:54.157: I/System.out(25193): 列明:display_name_source  列值:40
11-06 10:46:54.157: I/System.out(25193): 列明:photo_uri  列值:null
11-06 10:46:54.157: I/System.out(25193): 列明:photo_thumb_uri  列值:null
11-06 10:46:54.157: I/System.out(25193): 列明:contact_chat_capability  列值:null
11-06 10:46:54.157: I/System.out(25193): 列明:photo_id  列值:null


 }
}

Android Contacts Provider