低版本:
private void getContactsInLowVersion() {
Cursor tempContactsCursor = ContactActivity
.this.getContentResolver().query(Contacts.People.CONTENT_URI, null,
null, null, null);
System.out.println(tempContactsCursor.getCount() + "");
if (tempContactsCursor.moveToFirst()) {
do {
int peopleNameIndex = tempContactsCursor.getColumnIndex(Contacts.People.DISPLAY_NAME);
int mobileNumberIndex = tempContactsCursor.getColumnIndex(Contacts.Phones.NUMBER);
String name = tempContactsCursor.getString(peopleNameIndex);
String num = tempContactsCursor.getString(mobileNumberIndex);
contactNameList.add(name + "");
contactNumList.add(num + "");
} while (tempContactsCursor.moveToNext());
tempContactsCursor.close();
}
}
高版本:
private void getContactsInHighVersion() {
Cursor cur = getContentResolver().query(
ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
startManagingCursor(cur);
int num = cur.getCount();
System.out.println(num + "");
int count = 0;
while (cur.moveToNext()) {
count ++;
long id = cur.getLong(cur.getColumnIndex("_id"));
Cursor pcur = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "="
+ Long.toString(id), null, null);
// 处理多个号码的情况
String phoneNumbers = "";
while (pcur.moveToNext()) {
String strPhoneNumber = pcur
.getString(pcur
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
phoneNumbers += strPhoneNumber + ":";
}
phoneNumbers += "\n";
pcur.close();
String name = cur.getString(cur.getColumnIndex("display_name"));
contactNameList.add(name);
contactNumList.add(phoneNumbers);
}
cur.close();
}
本文介绍了在不同Android版本中获取手机联系人信息的方法。对于低版本系统,通过直接查询People.CONTENT_URI来获取联系人姓名及电话号码;而在高版本系统中,则通过ContactsContract.Contacts.CONTENT_URI来获取联系人的_id,并进一步查询ContactsContract.CommonDataKinds.Phone.CONTENT_URI以获取所有电话号码。
6111

被折叠的 条评论
为什么被折叠?



