您的位置:首页 > 移动开发 > Android开发

【Android】获取手机通讯录中的联系人信息

2014-07-30 03:20 295 查看
1.手机中的联系人信息是经常用到的一个功能,下面这段代码实现了获取联系人信息的功能

private List<Contact> initContactList() {
List<Contact> contactList = new ArrayList<Contact>();

// 查询联系人数据
Cursor cursor = getContentResolver().query(
ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
while (cursor.moveToNext()) {
Contact contact = new Contact();
// 获取联系人的Id
String contactId = cursor.getString(cursor
.getColumnIndex(ContactsContract.Contacts._ID));
// 获取联系人的姓名
String contactName = cursor.getString(cursor
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
contact.setContactName(contactName);

// 有联系人姓名得到对应的拼音
String pinyin = PinyinUtils.getPinyin(contactName);
contact.setPinyin(pinyin);

Cursor phoneCursor = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "="
+ contactId, null, null);

while (phoneCursor.moveToNext()) {
String phoneNumber = phoneCursor
.getString(phoneCursor
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
contact.setPhoneNumber(phoneNumber);
}

if (null != phoneCursor && !phoneCursor.isClosed()) {
phoneCursor.close();
}

contactList.add(contact);
}

if (null != cursor && !cursor.isClosed()) {
cursor.close();
}

Log.i("", contactList.toString());

return contactList;
}


参考:/article/3810409.html


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: