【问题标题】:Android: Retrieve Name, Phone Number, Email, Birthday from ContactAndroid:从联系人中检索姓名、电话号码、电子邮件、生日
【发布时间】:2018-03-16 06:41:10
【问题描述】:

我只能使用下面的代码获得姓名和生日。但我也需要电话号码和电子邮件。如果有人可以帮助我,那就太好了。谢谢。

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);
    }
}

【问题讨论】:

    标签: android android-contacts


    【解决方案1】:

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

    您需要询问电子邮件和电话的 mimetype,但请注意,对于同一联系人,这些附加信息将出现在不同的行中。 例如,对于有 2 部电话、3 封电子邮件和一个生日的联系人 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/events
    String 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);
    }
    

    【讨论】:

      猜你喜欢
      • 2011-03-05
      • 1970-01-01
      • 2014-02-12
      • 1970-01-01
      • 1970-01-01
      • 2020-03-01
      • 1970-01-01
      • 2016-01-02
      相关资源
      最近更新 更多