获取联系人的姓名、电话和邮箱
public void readContects() {
String string;
ContentResolver cr = getContentResolver();
Cursor c_name = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
null, null, null);
while (c_name.moveToNext()) {
/*
* 获取联系人ID
*/
String id = c_name.getString(c_name
.getColumnIndex(ContactsContract.Contacts._ID));
/*
* 获取联系人姓名
*/
String name = c_name.getString(c_name
.getColumnIndex(PhoneLookup.DISPLAY_NAME));
string = name + " ";
/*
* 获取与联系人ID相同的电话号码,可能不止一个
*/
Cursor c_number = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + id,
null, null);
while (c_number.moveToNext()) {
String number = c_number
.getString(c_number
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DATA1));
string += number + " ";
}
c_number.close();
/*
* 获取与联系人ID相同的电子邮件,可能不止一个
*/
Cursor c_email = cr.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + "=" + id,
null, null);
while (c_email.moveToNext()) {
String email = c_email
.getString(c_email
.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA1));
string += email + " ";
}
c_email.close();
string += "\n";
tvContacts.append(string);
}
c_name.close();
}
加权限:
<uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>
这段代码适用于sdk2.0或以上版本,并没有做错误处理,请注意使用!