欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

一个简单的获取通讯录联系人手机号

程序员文章站 2022-04-04 12:14:16
...

private ArrayList<SamContact> getAllContacts()
{
ArrayList<SamContact> arrayList = new ArrayList<SamContact>();
Cursor cur = getContentResolver().query(
ContactsContract.Contacts.CONTENT_URI,
null ,
null ,
null ,
ContactsContract.Contacts.DISPLAY_NAME
+ " COLLATE LOCALIZED ASC" );
if(cur.moveToFirst())
{
do{
SamContact samContact = new SamContact();
int idColumn = cur.getColumnIndex(ContactsContract.Contacts._ID);
int displayNameColumn = cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
// 获得联系人的ID号
String contactId = cur.getString(idColumn);
// 获得联系人姓名
String disPlayName = cur.getString(displayNameColumn);
System.out.println(disPlayName);
samContact.name = disPlayName;
// 查看该联系人有多少个电话号码。如果没有这返回值为0
int phoneCount = cur.getInt(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
if(phoneCount <1)
{
continue;
}
Cursor phones = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null ,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = " + contactId, null , null );
if (phones.moveToFirst())
{
do {
// 遍历所有的电话号码
String phoneNumber = phones
.getString(phones
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
int phoneType = phones
.getInt(phones
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
if(phoneType == ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE)
{
samContact.phone = phoneNumber;
arrayList.add(samContact);
break;
}
} while (phones.moveToNext());

}



}while(cur.moveToNext());
}
return arrayList;
}


public static class SamContact
{
public String name = "";
public String phone = "";
public boolean isChoosed = false;
}
相关标签: Mobile