从联系中重新获取SIP地址

时间:2018-03-11 15:45:28

标签: android android-contacts contactscontract

我需要得到这个字段:

enter image description here

但我正在努力处理我的代码:

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查询以找到此号码?

1 个答案:

答案 0 :(得分:1)

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

您目前正在进行太多查询只是为了从Data表中获取所有手机/电子邮件/ sip,您可以在一个简单查询中完成所有操作,并使用选择来指定MIMETYPE你对此感兴趣。

以下是示例代码,考虑将联系人Map从字符串列表切换到某个自定义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);
}