代码获取当前联系人的号码和姓名
public void test111(View view) {
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
if (cur != null && cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if (cur.getInt(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)) > 0) {
Cursor pCur = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]{id}, null);
if (pCur != null) {
while (pCur.moveToNext()) {
String phoneNo = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Logger.log(MainActivity.class, "!!!!!!!!! Name : " + name + ", Phone No: " + phoneNo, Logger.ERROR);
}
}
if (pCur != null) {
pCur.close();
}
}
}
}
}
但在下一个案例中问题,如果我需要从当前联系人那里收到电子邮件,我必须再创建一个cursor
Cursor cur1 = cr.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
new String[]{id}, null);
它会返回可用电子邮件的列表,但不会显示当前联系人的电子邮件
如果可用的电子邮件name, number, email
,如果电子邮件不可用name, number, null