如何从联系人列表中获取电子邮件地址?
Posted
技术标签:
【中文标题】如何从联系人列表中获取电子邮件地址?【英文标题】:how to get email address from contact list? 【发布时间】:2018-11-14 14:37:10 【问题描述】:如何从联系人列表中获取电子邮件地址。下面的代码我得到了号码和姓名并显示在列表中。但我想要号码、姓名和电子邮件地址,所以请检查下面的代码
ContentResolver contactResolver = context.getContentResolver();
Cursor c = contactResolver.query(ContactsContract.Contacts.CONTENT_URI, new String[] ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME, ContactsContract.Contacts.HAS_PHONE_NUMBER , null, null, null);
Cursor cursor = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
null, null, null);
if(cursor.getCount()>0)
while ( cursor.moveToNext())
String displayName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String number = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
// String photoUri = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.PHOTO_THUMBNAIL_URI));
String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
// String lookupKey = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
Log.e("TAG", " Name: " + displayName+"==>phone Number==>"+number);
contactNameArrayList.add(displayName);
contactNumberArrayList.add(number);
if (Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
Cursor pCur = contactResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] contactId , null);
while (pCur.moveToNext())
String phone = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
String type = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
String s = (String) ContactsContract.CommonDataKinds.Phone.getTypeLabel(context.getResources(), Integer.parseInt(type), "");
// Log.e("TAG", s + " phone: " + phone);
pCur.close();
Cursor emailCursor = contactResolver.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null,
null, null, null);
while (emailCursor.moveToNext())
String email = emailCursor.getString(emailCursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
int type = emailCursor.getInt(emailCursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
String s = (String) ContactsContract.CommonDataKinds.Email.getTypeLabel(context.getResources(), type, "");
Log.e("TAG", s + " email: " + email);
emailCursor.close();
cursor.close();
Log.e(TAG,"contactNameArrayList sIZE==>"+contactNameArrayList.size()+"contactNumberArrayList size==>"+contactNumberArrayList.size()+"contactEmailAddressArrayList size==>"+contactEmailAddressArrayList.size());
我为姓名和号码以及电子邮件 ID 创建了三个数组,但在上面的代码中我只有手机和姓名。当我在数组中添加姓名和手机号码时,因此同时将电子邮件 ID 特定手机号码存储在电子邮件 ID 数组中。所以请帮助...
【问题讨论】:
重复的问题有很多答案。 how to get email address rom contact list 【参考方案1】:您可以使用此方法从联系人那里获取电子邮件
public List<String> getEmail(int contactId)
List<String> emailStr = new ArrayList<String>();
ContentResolver cr = context.getContentResolver();
Cursor emailCur = cr.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
new String[]String.valueOf(contactId), null);
while (emailCur.moveToNext())
String email = emailCur.getString(
emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
emailStr.add(email);
System.out.println("Email " + email );
emailCur.close();
return emailStr;
【讨论】:
【参考方案2】:使用以下代码从一个联系人和电话簿中的所有联系人获取多个号码和电子邮件。
private void getContactsFromPhoneBook()
String unique_id = ApplicationConstant.phone_id;
System.out.println("Unique_ID : " + unique_id);
Contact_To_Sync contact_to_sync = new Contact_To_Sync();
List<Contacts> AllContact_toSync = new ArrayList<>();
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
System.out.println("Contacts Count : " + cur.getCount());
if (cur.getCount() > 0)
Contacts contacts = null;
List<String> Phones;
List<String> Emails;
while (cur.moveToNext())
contacts = new Contacts();
Phones = new ArrayList<>();
Emails = new ArrayList<>();
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String timeStamp = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.CONTACT_LAST_UPDATED_TIMESTAMP));
contacts.setContact_id(id);
contacts.setName(name);
System.out.println("name : " + name + ", ID : " + id + " Status : " + timeStamp);
// get the phone number
Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
new String[]id, null);
while (pCur.moveToNext())
String phone = pCur.getString(
pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
System.out.println("phone" + phone);
Phones.add(phone);
pCur.close();
// get email and type
Cursor emailCur = cr.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
new String[]id, null);
while (emailCur.moveToNext())
// This would allow you get several email addresses
// if the email addresses were stored in an array
String email = emailCur.getString(
emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
String emailType = emailCur.getString(
emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
Emails.add(email);
System.out.println("Email " + email + " Email Type : " + emailType);
emailCur.close();
contacts.setEmails(Emails);
contacts.setPhones(Phones);
AllContact_toSync.add(contacts);
contact_to_sync.setContacts(AllContact_toSync);
contact_to_sync.setPhone_device_name(unique_id);
if (!contact_to_sync.getContacts().isEmpty())
Sync_Contacts_To_Server(contact_to_sync);
【讨论】:
以上是关于如何从联系人列表中获取电子邮件地址?的主要内容,如果未能解决你的问题,请参考以下文章
使用 Google Scripts 从 Google People API 获取 google 联系人电子邮件目录中的电子邮件列表