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


private ArrayList getAllContacts()
{
ArrayList arrayList = new ArrayList();
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;
}

你可能感兴趣的:(android)