/** 获取库Phone表字段 **/
private static final String[] PHONES_PROJECTION = new String[] { Phone.DISPLAY_NAME, Phone.NUMBER };
private List<ContactsModel> contactsModels = new ArrayList<ContactsModel>();
/** 得到手机通讯录联系人信息 **/
private void getPhoneContacts() {
ContentResolver resolver = mContext.getContentResolver();
// 获取手机联系人
Cursor phoneCursor = resolver.query(Phone.CONTENT_URI, PHONES_PROJECTION, null, null, null);
if (phoneCursor != null) {
while (phoneCursor.moveToNext()) {
ContactsModel contactsModel = new ContactsModel();
String contactName = phoneCursor.getString(0);
// 得到手机号码
String phoneNumber = phoneCursor.getString(1);
// 当手机号码为空的或者为空字段 跳过当前循环
if (TextUtils.isEmpty(phoneNumber))
continue;
// 得到联系人名称
contactsModel.setContactName(contactName);
contactsModel.setPhoneNumber(phoneNumber);
contactsModels.add(contactsModel);
}
phoneCursor.close();
}
}
/** 得到手机SIM卡联系人人信息 **/
private void getSIMContacts() {
ContentResolver resolver = mContext.getContentResolver();
// 获取Sims卡联系人
Uri uri = Uri.parse("content://icc/adn");
Cursor phoneCursor = resolver.query(uri, PHONES_PROJECTION, null, null, null);
if (phoneCursor != null) {
while (phoneCursor.moveToNext()) {
ContactsModel contactsModel = new ContactsModel();
// 得到手机号码
String contactName = phoneCursor.getString(0);
String phoneNumber = phoneCursor.getString(1);
// 当手机号码为空的或者为空字段 跳过当前循环
if (TextUtils.isEmpty(phoneNumber))
continue;
contactsModel.setContactName(contactName);
contactsModel.setPhoneNumber(phoneNumber);
contactsModels.add(contactsModel);
}
phoneCursor.close();
}
}
联系人model类
public class ContactsModel {
private String contactName;
private String phoneNumber;
public String getContactName() {
return contactName;
}
public void setContactName(String contactName) {
this.contactName = contactName;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
}