在这里我们先创建一个联系人类,包含人名,电话号码,头像等,当然你还可以包含别的,这里只是一个例子
public class MyContacts {
//联系人电话号码
private String phoneNumber;
//联系人名称
private String phoneName;
//联系人头像
Bitmap contactPhoto;
//。。。。。。set/get方法。。。。。
}
然后在AndroidManifest.xml中设置权限
读取联系人权限: <uses-permission android:name="android.permission.READ_CONTACTS" />
读写联系人权限: <uses-permission android:name="android.permission.WRITE_CONTACTS" />
然后是方法,获取联系人的姓名,电话号码,头像,返回一个集合
/**
*
* 得到手机通讯录联系人信息
*
**/
//这里强调一下query第二个参数PHONES_PROJECTION 下面第10行
//它的意思是只去表中找 显示名称 电话号码 头像ID 联系人ID 这4个数据 ,如果你须要其它数据 的话 就须要修改这里PHONES_PROJECTION。
private static final String[] PHONES_PROJECTION = new String[] {
Phone.DISPLAY_NAME, Phone.NUMBER, Photo.PHOTO_ID, Phone.CONTACT_ID };
/** 得到本机本地联系人信息 **/
private ArrayList<MyContacts> getPhoneContacts() {
ArrayList<MyContacts> arrayList = new ArrayList<MyContacts>();
//获得ContentResolver 实例
ContentResolver resolver = this.getContentResolver();
// 获取手机联系人
Cursor phoneCursor = resolver.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
PHONES_PROJECTION, null, null, null);
//判断游标是否为空
if (phoneCursor != null) {
while (phoneCursor.moveToNext()) {
MyContacts myContacts = new MyContacts();
// 得到手机号码
String phoneNumber = phoneCursor
.getString(phoneCursor
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
// 当手机号码为空的或者为空字段 跳过当前循环
if (TextUtils.isEmpty(phoneNumber))
continue;
// 得到联系人名称
String contactName = phoneCursor
.getString(phoneCursor
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
// 得到联系人ID
Long contactid = phoneCursor
.getLong(phoneCursor
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));
// 得到联系人头像ID
Long photoid = phoneCursor
.getLong(phoneCursor
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_ID));
// 得到联系人头像Bitamp
Bitmap contactPhoto = null;
// photoid 大于0 表示联系人有头像 如果没有给此人设置头像则给他一个默认的
if (photoid > 0) {
Uri uri = ContentUris.withAppendedId(
ContactsContract.Contacts.CONTENT_URI, contactid);
InputStream input = ContactsContract.Contacts
.openContactPhotoInputStream(resolver, uri);
contactPhoto = BitmapFactory.decodeStream(input);
} else {
contactPhoto = BitmapFactory.decodeResource(getResources(),
R.drawable.ic_launcher);
}
myContacts.setPhoneName(contactName);
myContacts.setPhoneNumber(phoneNumber);
myContacts.setContactPhoto(contactPhoto);
arrayList.add(myContacts);
}
phoneCursor.close();
}
return arrayList;
}
/** 得到手机SIM卡联系人信息 **/
private ArrayList<MyCardContacts> getSIMContacts() {
ArrayList<MyCardContacts> list = new ArrayList<MyCardContacts>();
ContentResolver resolver = this.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()) {
MyCardContacts cardContacts = new MyCardContacts();
// 得到手机号码
String phoneNumber = phoneCursor
.getString(phoneCursor
.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
// 当手机号码为空的或者为空字段 跳过当前循环
if (TextUtils.isEmpty(phoneNumber))
continue;
// 得到联系人名称
String contactName = phoneCursor
.getString(phoneCursor
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
cardContacts.setName(contactName);
cardContacts.setNumber(phoneNumber);
list.add(cardContacts);
}
phoneCursor.close();
}
return list;
}