仅从联系人列表中获取电子邮件地址 Android
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10117049/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get only email address from contact list Android
提问by Krishna Suthar
I want to display only those contact names whose email address is present. Otherwise that contact name should not be displayed in List. How can I do this? Can anybody please help me?
我只想显示那些电子邮件地址存在的联系人姓名。否则,该联系人姓名不应显示在列表中。我怎样才能做到这一点?有人可以帮我吗?
回答by Marcin
Here is my super fast query to pull email addresses. It is much faster than pulling all contact columns as suggested by other answers...
这是我提取电子邮件地址的超快速查询。它比其他答案所建议的拉动所有联系列要快得多......
public ArrayList<String> getNameEmailDetails() {
ArrayList<String> emlRecs = new ArrayList<String>();
HashSet<String> emlRecsHS = new HashSet<String>();
Context context = getActivity();
ContentResolver cr = context.getContentResolver();
String[] PROJECTION = new String[] { ContactsContract.RawContacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts.PHOTO_ID,
ContactsContract.CommonDataKinds.Email.DATA,
ContactsContract.CommonDataKinds.Photo.CONTACT_ID };
String order = "CASE WHEN "
+ ContactsContract.Contacts.DISPLAY_NAME
+ " NOT LIKE '%@%' THEN 1 ELSE 2 END, "
+ ContactsContract.Contacts.DISPLAY_NAME
+ ", "
+ ContactsContract.CommonDataKinds.Email.DATA
+ " COLLATE NOCASE";
String filter = ContactsContract.CommonDataKinds.Email.DATA + " NOT LIKE ''";
Cursor cur = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, PROJECTION, filter, null, order);
if (cur.moveToFirst()) {
do {
// names comes in hand sometimes
String name = cur.getString(1);
String emlAddr = cur.getString(3);
// keep unique only
if (emlRecsHS.add(emlAddr.toLowerCase())) {
emlRecs.add(emlAddr);
}
} while (cur.moveToNext());
}
cur.close();
return emlRecs;
}
I tried the code provided by 'Agarwal Shankar' but it took about 4 seconds to get contacts on my test device, and this code took about 0.04 sec.
我尝试了“Agarwal Shankar”提供的代码,但在我的测试设备上获取联系人大约需要 4 秒,而此代码大约需要 0.04 秒。
回答by Shankar Agarwal
public ArrayList<String> getNameEmailDetails(){
ArrayList<String> names = new ArrayList<String>();
ContentResolver cr = 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()) {
//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);
if(email!=null){
names.add(name);
}
}
cur1.close();
}
}
return names;
}
the above method return an arraylist of names which has email id.
上面的方法返回一个包含电子邮件 ID 的名称数组列表。
回答by Shankar Agarwal
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
Cursor emailCur = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,null,ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", new String[]{id},null);
while (emailCur.moveToNext()) {
String email = emailCur.getString( emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
Log.e("Email",name+" "+email);
}
emailCur.close();
}
}
回答by DariusL
Another solution.
另一种解决方案。
private static final Uri URI_CONTACT_DATA = ContactsContract.Data.CONTENT_URI;
private static final String COLUMN_EMAIL = ContactsContract.CommonDataKinds.Email.ADDRESS;
private static final String COLUMN_DISPLAY_NAME = ContactsContract.Contacts.DISPLAY_NAME_PRIMARY;
private static final String COLUMN_MIMETYPE = ContactsContract.Data.MIMETYPE;
private static final String[] PROJECTION = {
COLUMN_DISPLAY_NAME,
COLUMN_EMAIL,
COLUMN_MIMETYPE
};
private Cursor getCursor() {
ContentResolver resolver = context.getContentResolver();
String selection = COLUMN_MIMETYPE + "=?";
final String[] selectionArgs = {ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE};
return resolver.query(URI_CONTACT_DATA, PROJECTION, selection, selectionArgs, null);
}
The issue is that the table at ContactsContract.Contacts.CONTENT_URI
holds the entire contact database. This includes phone numbers, emails, organisations, and even completely custom data, so you cannot use it without filtering with ContactsContract.Data.MIMETYPE
. A row in this database holds a value (or values, it has 15 generic columns) related to a certain account, so you might need to group them yourself. I needed this to autocomplete emails, so the format (email per row) was perfect.
问题在于表 atContactsContract.Contacts.CONTENT_URI
包含整个联系人数据库。这包括电话号码、电子邮件、组织,甚至完全自定义的数据,因此如果不使用ContactsContract.Data.MIMETYPE
. 此数据库中的一行包含与某个帐户相关的一个值(或多个值,它有 15 个通用列),因此您可能需要自己将它们分组。我需要它来自动完成电子邮件,因此格式(每行电子邮件)非常完美。
回答by DragonFire
Declare A Global Variable
声明一个全局变量
// Hash Maps
Map<String, String> nameEmailMap = new HashMap<String, String>();
Then Use The Function Below
然后使用下面的函数
private void getEmailIDs() {
Cursor emails = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, null, null, null);
// Loop Through All The Emails
while (emails.moveToNext()) {
String name = emails.getString(emails.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String email = emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.ADDRESS));
// Enter Into Hash Map
nameEmailMap.put(email, name);
}
// Get The Contents of Hash Map in Log
for (Map.Entry<String, String> entry : nameEmailMap.entrySet()) {
String key = entry.getKey();
Log.d(TAG, "Email :" + key);
String value = entry.getValue();
Log.d(TAG, "Name :" + value);
}
emails.close();
}
Remember in the above example the key is email and value is name so read your contents like [email protected]>Mahatma Gandhi instead of Mahatma Gandhi->[email protected]
请记住,在上面的示例中,键是电子邮件,值是名称,因此请阅读您的内容,例如 [email protected]>Mahatma Gandhi 而不是 Mahatma Gandhi->[email protected]
回答by Aneh Thakur
Here is a simple way to get email id of contact from contact list. You need to pass contact id of user in below method and it will return you email id if exists
这是从联系人列表中获取联系人电子邮件 ID 的简单方法。您需要在下面的方法中传递用户的联系 ID,如果存在,它将返回您的电子邮件 ID
public String getEmail(String contactId) {
String emailStr = "";
final String[] projection = new String[]{ContactsContract.CommonDataKinds.Email.DATA,
ContactsContract.CommonDataKinds.Email.TYPE};
Cursor emailq = managedQuery(ContactsContract.CommonDataKinds.Email.CONTENT_URI, projection, ContactsContract.Data.CONTACT_ID + "=?", new String[]{contactId}, null);
if (emailq.moveToFirst()) {
final int contactEmailColumnIndex = emailq.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA);
while (!emailq.isAfterLast()) {
emailStr = emailStr + emailq.getString(contactEmailColumnIndex) + ";";
emailq.moveToNext();
}
}
return emailStr;
}
And also if you want to learn how to get contact list in your app follow this link: show contact list in app android - trinitytuts
此外,如果您想了解如何在您的应用中获取联系人列表,请点击此链接:在应用 android - trinitytuts 中显示联系人列表