这篇文章给大家介绍Android中是如何获取手机联系人的,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。
Android 获取系统联系人信息的实例
一、获取手机联系人姓名及手机号
//跳转到系统联系人应用
Intent intent = new Intent(Intent.ACTION_PICK,
ContactsContract.Contacts.CONTENT_URI);
try {
startActivityForResult(intent, Contacts1RequestCode);
} catch (Exception e) {
LogManager.e("打开联系人信息失败");
}
添加权限申请
<uses-permission android:name="android.permission.READ_CONTACTS" />
选择联系人并返回
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (Contacts1RequestCode == requestCode) {// 取联系信息返回
if (resultCode == RESULT_OK) {
Uri contactData = data.getData();
Cursor cursor = getContentResolver().query(contactData, null,
null, null, null);
//Key联系人姓名,Value联系人手机号
Map<String, String> phoneMap = this.getContactPhone(cursor);
if (!cursor.isClosed()) {
cursor.close();
}
if (null != phoneMap && !phoneMap.isEmpty()) {
Set<String> keySet = phoneMap.keySet();
if (null != keySet && !keySet.isEmpty()) {
Object[] keys = keySet.toArray();
String phoneName = (String) keys[0];
String phoneNo = phoneMap.get(phoneName);
}
}
}
}
}
/**
* 获取联系人姓名及手机号
*
* @param cursor
* @return Key为联系人姓名,Value为联系人手机号
*/
private Map<String, String> getContactPhone(Cursor cursor) {
Map<String, String> resultMap = new HashMap<String, String>();
String phoneName = null;// 姓名
String mobilePhoneNo = null;// 手机号
if (null != cursor) {
cursor.moveToFirst();
// 获得联系人的ID号
int idFieldIndex = cursor
.getColumnIndex(ContactsContract.Contacts._ID);
String contactId = cursor.getString(idFieldIndex);
// 联系人姓名
int idphoneNameIndex = cursor
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
phoneName = cursor.getString(idphoneNameIndex);
// 获得联系人的电话号码的cursor;
Cursor allPhones = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=?",
new String[] { contactId }, null);
// 所以联系电话(包话电话和手机号)
List<String> allPhoneNumList = new ArrayList<String>();
if (allPhones.moveToFirst()) {
// 遍历所有的电话号码
for (; !allPhones.isAfterLast(); allPhones.moveToNext()) {
int telNoTypeIndex = allPhones
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE);
int telNoType = allPhones.getInt(telNoTypeIndex);
int telNoIndex = allPhones
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
String telNo = allPhones.getString(telNoIndex);
allPhoneNumList.add(telNo);
if (2 == telNoType) {// 手机号(原生态的SDK定义:mobile是2,home是1,work是3,other是7)
mobilePhoneNo = telNo;
break;
}
}
if (!allPhones.isClosed()) {
allPhones.close();
}
if (null == mobilePhoneNo) {// 没有存贮手机号
if (!allPhoneNumList.isEmpty()) {// 存在其它号码
for (String tel : allPhoneNumList) {
if (VerifyKit.isLegal(FormatType.MobilePhone, tel)) {// 取属于手机号格式
mobilePhoneNo = tel;
break;
}
}
}
}
}
if (!cursor.isClosed()) {
cursor.close();
}
resultMap.put(phoneName, mobilePhoneNo);
}
return resultMap;
}
关于Android中是如何获取手机联系人的就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。