遍历通讯录中所有的列名及对应的值:
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
Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)
应用:Cursor cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI,
new String[]{ContactsContract.Contacts.DISPLAY_NAME},
ContactsContract.Contacts.DISPLAY_NAME + "=?",
new String[]{"张三"}, " DESC"); //默认为ASC
COLUMN_CONTACT_ID + "=? AND " + COLUMN_MIMETYPE + "=?",
Android 学习笔记 Contacts (三) Contacts 查找,增加,更新,删除联系人:
http://blog.csdn.net/wssiqi/article/details/8157399
android根据电话号码查询联系人名称,导出通讯录所有联系人的方法:
http://www.cnblogs.com/zdz8207/archive/2012/11/09/2762893.html