不同版本SDK电话本内容的获取(电话,姓名等)

本文介绍了在不同Android版本中获取手机联系人信息的方法。对于低版本系统,通过直接查询People.CONTENT_URI来获取联系人姓名及电话号码;而在高版本系统中,则通过ContactsContract.Contacts.CONTENT_URI来获取联系人的_id,并进一步查询ContactsContract.CommonDataKinds.Phone.CONTENT_URI以获取所有电话号码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

低版本:

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();
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值