Android:从联系人中检索姓名,电话号码,电子邮件,生日

Android:从联系人中检索姓名,电话号码,电子邮件,生日,第1张

概述我只得到名字和生日使用下面的代码.但我需要电话号码和电子邮件也.如果有人能帮助我,那将是很棒的.谢谢.privatevoidgetContacts(){Uriuri=ContactsContract.Data.CONTENT_URI;String[]projection=newString[]{ContactsContract.Contacts.DISP

我只得到名字和生日使用下面的代码.但我需要电话号码和电子邮件也.如果有人能帮助我,那将是很棒的.谢谢.

private voID getContacts() {    Uri uri = ContactsContract.Data.CONTENT_URI;    String[] projection = new String[]{            ContactsContract.Contacts.disPLAY_name,            ContactsContract.CommonDataKinds.Event.CONTACT_ID,            ContactsContract.CommonDataKinds.Phone.NUMBER,            ContactsContract.CommonDataKinds.Email.DATA,            ContactsContract.CommonDataKinds.Event.START_DATE    };    String where = ContactsContract.Data.MIMETYPE + "= ? AND " +            ContactsContract.CommonDataKinds.Event.TYPE + "=" +            ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY;    String[] selectionArgs = new String[]{            ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE    };    String sortOrder = null;    ContentResolver contentResolver = this.getActivity().getContentResolver();    Cursor cursor = contentResolver.query(uri, projection, where, selectionArgs, sortOrder);    int nameColumn = cursor.getColumnIndex(ContactsContract.Contacts.disPLAY_name);    int numberColumn = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);    int emailColumn = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA);    int bithDayColumn = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Event.START_DATE);    while (cursor.movetoNext()) {        String name = cursor.getString(nameColumn);        String number = cursor.getString(numberColumn);        String email = cursor.getString(emailColumn);        String birthDay = cursor.getString(bithDayColumn);        Log.d(TAG, "Birthday: " + birthDay);    }}

解决方法:

在您的投影中,您只是将查询限制为MIMETYPE CommonDataKinds.Event.CONTENT_ITEM_TYPE行,因此您只能获得生日.

您需要询问电子邮件和手机mimetypes,但请注意,这些附加信息将分别出现在同一联系人的行中.
例如,对于有2部电话,3封电子邮件和1个生日的联系人A,您将在游标中获得6个结果.因此,您需要使用CONTACT_ID字段将它们全部组合在一起.

这里有一些简单的代码可以帮助您入门,打印生成的HashMap,您将获得每个联系人的所有姓名,电子邮件,电话和生日:

Map<Long, List<String>> contacts = new HashMap<Long, List<String>>();String[] projection = {Data.CONTACT_ID, Data.disPLAY_name, Data.MIMETYPE, Data.DATA1, Data.DATA2, Data.DATA3};// query only emails/phones/eventsString selection = Data.MIMETYPE + " IN ('" + Phone.CONTENT_ITEM_TYPE + "', '" + Event.CONTENT_ITEM_TYPE"', '" + Email.CONTENT_ITEM_TYPE + "')";ContentResolver cr = getContentResolver();Cursor cur = cr.query(Data.CONTENT_URI, projection, selection, null, null);while (cur != null && cur.movetoNext()) {    long ID = cur.getLong(0);    String name = cur.getString(1); // full name    String mime = cur.getString(2); // type of data (phone / birthday / email)    String data = cur.getString(3); // the actual info, e.g. +1-212-555-1234    String kind = "unkNown";    switch (mime) {        case Phone.CONTENT_ITEM_TYPE:             kind = "phone";             break;        case Event.CONTENT_ITEM_TYPE:             kind = "birthday";            break;        case Email.CONTENT_ITEM_TYPE:             kind = "email";            break;    }    Log.d(TAG, "got " + ID + ", " + name + ", " + kind + " - " + data);    // add info to existing List if this contact-ID was already found, or create a new List in case it's new    List<String> infos;    if (contacts.containsKey(ID)) {        infos = contacts.get(ID);    } else {        infos = new ArrayList<String>();        infos.add("name = " + name);        contacts.put(ID, infos);    }    infos.add(kind + " = " + data);}
总结

以上是内存溢出为你收集整理的Android:从联系人中检索姓名,电话号码,电子邮件,生日全部内容,希望文章能够帮你解决Android:从联系人中检索姓名,电话号码,电子邮件,生日所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1100762.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存