请看一看 :-
public static ArrayList<ContactsEntityBean> getContactDetails(
Context mContext) {
ArrayList<ContactsEntityBean> contactList = new ArrayList<ContactsEntityBean>();
ContentResolver cr = mContext.getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(cur
.getColumnIndex(ContactsContract.Contacts._ID));
Cursor cur1 = cr.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null, ContactsContract.CommonDataKinds.Email.CONTACT_ID
+ " = ?", new String[] {
id
}, null);
while (cur1.moveToNext()) {
ContactsEntityBean contactsEntityBean = new ContactsEntityBean();
// to get the contact names
String name = cur1
.getString(cur1
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
// Log.e("Name :", name);
String email = cur1
.getString(cur1
.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
// Log.e("Email", email);
contactsEntityBean.setName(name);
contactsEntityBean.setEmail(email);
if (email != null) {
contactList.add(contactsEntityBean);
}
}
cur1.close();
}
}
return contactList;
}
这个方法是从同一个用户返回多个联系人假设如果我为同一个用户存储了 abc@gmail.com,abc@gmail.com 所以它返回 abc@gmail.com& abc@gmail.com 但我只想要一个记录 abc@ gmail.com
public static ArrayList<SearchEntityBean> getContactEmailDetails(
Context mContext) {
ArrayList<SearchEntityBean> contactList = new ArrayList<SearchEntityBean>();
try {
ContentResolver cr = mContext.getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String email = "";
String id = cur.getString(cur
.getColumnIndex(ContactsContract.Contacts._ID));
Cursor cur1 = cr.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID
+ " = ?", new String[] {
id
}, null);
SearchEntityBean contactsEntityBean = new SearchEntityBean();
while (cur1.moveToNext()) {
// to get the contact names
String name = cur1
.getString(cur1
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String image = cur1
.getString(cur1
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_ID));
String mail = cur1
.getString(cur1
.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
if (mail != null) {
if (!mail.equalsIgnoreCase(LoginPreferenceClass
.getEmailID(mContext)))
email = email + mail + ",";
}
// Log.e("rohit", "Contact Email :" + email);
contactsEntityBean.setName(name);
contactsEntityBean.setImage(image);
}
if (email != null) {
if (email.length() > 0) {
if (email.split(",").length > 1) {
contactsEntityBean.setMutipleEmail(true);
}
contactsEntityBean.setUserType("2");
contactsEntityBean.setContactId(id);
contactsEntityBean.setEmail(email);
contactList.add(contactsEntityBean);
}
}
cur1.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
HashSet<SearchEntityBean> hs = new HashSet<SearchEntityBean>();
hs.addAll(contactList);
contactList.clear();
contactList.addAll(hs);
return contactList;
}