获取安卓联系人
我正在尝试获取所有联系人和电子邮件地址的列表。
目前我正在使用此代码,但是我收到了很多重复的姓名和电子邮件。我想要的只是一个人和一封电子邮件。有没有办法可以在查询联系人时合并联系人?
我也只是在寻找一个> 2.0解决方案。
private void init() {
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));
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
Cursor emailCur = cr.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();
}
}
Im trying to get a list of all the contacts and there email address.
Currently im using this code, however I get a lot of duplicate names and emails. What i want is just one person and one email. Is there a way I can merge contacts while querying for them?
Also im just looking for a > 2.0 solution.
private void init() {
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));
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
Cursor emailCur = cr.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();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试下面的工作代码。
Try out the working code below.