从联系人中检索 IP 地址

Retriving SIP adress from contact

我需要获取这个字段:

但是我正在为我的代码苦苦挣扎:

private void getContactList() {

    ContentResolver cr = getContentResolver();
    Cursor  cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);


    if ((cur != null ? cur.getCount() : 0) > 0) {
        while (cur != null && cur.moveToNext()) {

            Contact contact = new Contact();

            String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
            String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            String photoURI = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.PHOTO_URI));
            String photoThumbURI = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.PHOTO_THUMBNAIL_URI));


            //Phones
            Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                    null,
                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
                    new String[]{id}, null);


            while (pCur.moveToNext()) {

                String phoneNum = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                int phoneType = pCur.getInt(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));

            }

            ///Emails
            Cursor pCur2 = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,
                    null,
                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
                    new String[]{id}, null);

            while (pCur2.moveToNext())
            {

                String em = pCur2.getString(pCur2.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
                Log.d("DTAG","em: "+em);

            }


            ///Sip
            Cursor pCur3 = cr.query(/*What goes here?*/,
                    null,
                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
                    new String[]{id}, null);

            while (pCur3.moveToNext())
            {
                String em = pCur3.getString(pCur3.getColumnIndex(ContactsContract.CommonDataKinds.SipAddress.DATA));
            }

            contact.setId(id);
            contact.setName(name);
            contact.setImageURI(photoURI);
            contact.setThumbnailURI(photoThumbURI);

            pCur.close();

        }
    }
    if (cur != null) {
        cur.close();
    }

}

如何解决 SIP 查询以查找此号码?

来自 Phone/Email/etc CONTENT_URI 的所有数据都包含在一个名为 Data 的大 table 中。查询 table 允许与 Contacts table 隐式连接,因此您也可以获得 CONTACT_IDDISPLAY_NAME 等。

您目前正在执行太多查询,只是为了从数据 table 中获取所有 phones/email/sips,您可以在一个简单的查询中完成所有操作,并使用选择来指定哪个 MIMETYPE你感兴趣的。

这是示例代码,考虑将联系人映射从字符串列表切换到某个自定义 POJO 对象:

Map<Long, List<String>> contacts = new HashMap<Long, List<String>>();

String[] projection = {Data.CONTACT_ID, Data.DISPLAY_NAME, Data.MIMETYPE, Data.DATA1, Data.PHOTO_URI, Data.PHOTO_THUMBNAIL_URI};

// query only emails/phones/sip
String selection = Data.MIMETYPE + " IN ('" + 
    Phone.CONTENT_ITEM_TYPE + "', '" + 
    Email.CONTENT_ITEM_TYPE + "', '" + 
    SipAddress.CONTENT_ITEM_TYPE + "')";

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 / email / sip)
    String data = cur.getString(3); // the actual info, e.g. +1-212-555-1234
    String photo = cur.getString(4);
    String thumb = cur.getString(5);

    String kind = "unknown";

    switch (mime) {
        case Phone.CONTENT_ITEM_TYPE: 
            kind = "phone"; 
            break;
        case Email.CONTENT_ITEM_TYPE: 
            Event = "email";
            break;
        case SipAddress.CONTENT_ITEM_TYPE: 
            Event = "sip";
            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 {
        // create a new contact object
        infos = new ArrayList<String>();
        infos.add("name = " + name);
        infos.add("photo = " + photo);
        infos.add("thumb = " + thumb);
        contacts.put(id, infos);
    }
    infos.add(kind + " = " + data);
}