android++获取联系人,android获取所有联系人

//GET CONTACTLIST WITH ALL FIELD...       public ArrayList  getReadContacts() {

ArrayList  contactList = new ArrayList  ();

ContentResolver cr = getContentResolver();

Cursor mainCursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);

if (mainCursor != null) {

while (mainCursor.moveToNext()) {

ContactItem contactItem = new ContactItem();

String id = mainCursor.getString(mainCursor.getColumnIndex(ContactsContract.Contacts._ID));

String displayName = mainCursor.getString(mainCursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

Uri contactUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, Long.parseLong(id));

Uri displayPhotoUri = Uri.withAppendedPath(contactUri, ContactsContract.Contacts.Photo.DISPLAY_PHOTO);

//ADD NAME AND CONTACT PHOTO DATA...

contactItem.setDisplayName(displayName);

contactItem.setPhotoUrl(displayPhotoUri.toString());

if (Integer.parseInt(mainCursor.getString(mainCursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {

//ADD PHONE DATA...

ArrayList  arrayListPhone = new ArrayList  ();

Cursor phoneCursor = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] {

id            }, null);

if (phoneCursor != null) {

while (phoneCursor.moveToNext()) {

PhoneContact phoneContact = new PhoneContact();

String phone = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

phoneContact.setPhone(phone);

arrayListPhone.add(phoneContact);

}

}

if (phoneCursor != null) {

phoneCursor.close();

}

contactItem.setArrayListPhone(arrayListPhone);

//ADD E-MAIL DATA...

ArrayList  arrayListEmail = new ArrayList  ();

Cursor emailCursor = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", new String[] {

id            }, null);

if (emailCursor != null) {

while (emailCursor.moveToNext()) {

EmailContact emailContact = new EmailContact();

String email = emailCursor.getString(emailCursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));

emailContact.setEmail(email);

arrayListEmail.add(emailContact);

}

}

if (emailCursor != null) {

emailCursor.close();

}

contactItem.setArrayListEmail(arrayListEmail);

//ADD ADDRESS DATA...

ArrayList  arrayListAddress = new ArrayList  ();

Cursor addrCursor = getContentResolver().query(ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_URI, null, ContactsContract.CommonDataKinds.StructuredPostal.CONTACT_ID + " = ?", new String[] {

id            }, null);

if (addrCursor != null) {

while (addrCursor.moveToNext()) {

PostalAddress postalAddress = new PostalAddress();

String city = addrCursor.getString(addrCursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.CITY));

String state = addrCursor.getString(addrCursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.REGION));

String country = addrCursor.getString(addrCursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY));

postalAddress.setCity(city);

postalAddress.setState(state);

postalAddress.setCountry(country);

arrayListAddress.add(postalAddress);

}

}

if (addrCursor != null) {

addrCursor.close();

}

contactItem.setArrayListAddress(arrayListAddress);

}

contactList.add(contactItem);

}

}

if (mainCursor != null) {

mainCursor.close();

}

return contactList;

}

//MODEL...

public class ContactItem {

private String displayName;

private String photoUrl;

private ArrayList arrayListPhone = new ArrayList<>();

private ArrayList arrayListEmail = new ArrayList<>();

private ArrayList arrayListAddress = new ArrayList<>();

public String getDisplayName() {

return displayName;

}

public void setDisplayName(String displayName) {

this.displayName = displayName;

}

public String getPhotoUrl() {

return photoUrl;

}

public void setPhotoUrl(String photoUrl) {

this.photoUrl = photoUrl;

}

public ArrayList getArrayListPhone() {

return arrayListPhone;

}

public void setArrayListPhone(ArrayList arrayListPhone) {

this.arrayListPhone = arrayListPhone;

}

public ArrayList getArrayListEmail() {

return arrayListEmail;

}

public void setArrayListEmail(ArrayList arrayListEmail) {

this.arrayListEmail = arrayListEmail;

}

public ArrayList getArrayListAddress() {

return arrayListAddress;

}

public void setArrayListAddress(ArrayList arrayListAddress) {

this.arrayListAddress = arrayListAddress;

}

}

public class EmailContact

{

private String email = "";

public String getEmail() {

return email;

}

public void setEmail(String email) {

this.email = email;

}

}

public class PhoneContact

{

private String phone="";

public String getPhone()

{

return phone;

}

public void setPhone(String phone) {

this.phone = phone;

}

}

public class PostalAddress

{

private String city="";

private String state="";

private String country="";

public String getCity() {

return city;

}

public void setCity(String city) {

this.city = city;

}

public String getState() {

return state;

}

public void setState(String state) {

this.state = state;

}

public String getCountry() {

return country;

}

public void setCountry(String country) {

this.country = country;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值