我在从手机联系人加载电子邮件地址时遇到问题
我正在尝试从手机联系人生成电子邮件列表,但在专门获取电子邮件方面遇到了麻烦。我可以获取姓名和电话号码,但不能获取电子邮件。循环结束时 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));
}
I am trying to generate a list of emails from the phone's contacts but I have having trouble specifically getting emails. I can get names and phone numbers but not emails. The ArrayList sizes are both 0 at the end of the loop. Can anyone spot what I am doing wrong, or tell me how emails differ from phone numbers, names and other information?
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));
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用此代码从设备获取电子邮件。
use this code to get email from device.