我正在尝试从手机的联系人生成电子邮件列表,但我无法专门收到电子邮件。我可以获取姓名和电话号码,但不能收到电子邮件。 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));
}
答案 0 :(得分:2)
使用此代码从设备获取电子邮件。
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();