我无法从电话联系人中加载电子邮件地址
Posted
技术标签:
【中文标题】我无法从电话联系人中加载电子邮件地址【英文标题】:I'm having trouble loading email addresses from the Phone's contacts 【发布时间】:2011-08-23 02:51:45 【问题描述】:我正在尝试从手机的联系人中生成电子邮件列表,但我在专门接收电子邮件时遇到了麻烦。我可以得到姓名和电话号码,但不能得到电子邮件。 ArrayList 大小在循环结束时均为 0。谁能发现我做错了什么,或者告诉我电子邮件与电话号码、姓名和其他信息有何不同?
ContentResolver cr = getContentResolver();
Cursor emailCur = cr.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
null, null);
ArrayList<String> emails = new ArrayList<String>();
ArrayList<String> emailTypes = new ArrayList<String>();
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);
emailTypes.add(emailType);
emailCur.close();
Log.i("Emails Count", Integer.toString(emails.size()));
for (int i = 0; i < emails.size(); i++)
Log.i("Emails " + i, emails.get(i));
Log.i("EmailTypes Count", Integer.toString(emailTypes.size()));
for (int i = 0; i < emailTypes.size(); i++)
Log.i("EmailTypes " + i, emailTypes.get(i));
【问题讨论】:
【参考方案1】:使用此代码从设备获取电子邮件。
ContentResolver cr = getContentResolver();
cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,null, null, null);
emailIndex=0;
if (cur.getCount() > 0)
while (cur.moveToNext())
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
Cursor emails = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID+ " = " + id, null, null);
while (emails.moveToNext())
// This would allow you get several email addresses
String emailAddress = emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
Log.v(name+"==>", emailAddress);
emails.close();
cur.close();
【讨论】:
以上是关于我无法从电话联系人中加载电子邮件地址的主要内容,如果未能解决你的问题,请参考以下文章