收到联系人的所有电话号码

时间:2017-07-22 15:42:18

标签: android cursor android-contacts

我已经看到了其他类似的问题,但都没有效果。 通过下面的代码,我可以获得联系人的默认电话号码,但不是全部。

Cursor contact = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId, null, null);
contact.moveToFirst();

phoneNumberList.add(contact.getString(contact.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));

然后我尝试创建多个游标,其中包含有关手机类型的更多详细信息,然后将其添加到列表中:

Cursor contact = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER}, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ? AND " + ContactsContract.CommonDataKinds.Phone.TYPE + " = " + ContactsContract.CommonDataKinds.Phone.TYPE_HOME, new String[]{contactID}, null);

但不幸的是,它没有解决任何问题。如果有人帮我解决这个问题,我将不胜感激。

1 个答案:

答案 0 :(得分:0)

我假设您想要获得所有类型的电话号码(家庭,移动电话,主要电话等)。

以下是如何操作(手机列表将打印到log =:

String projection = new String[] { Phone.NUMBER, Phone.TYPE, Phone.LABEL };
String selection = Phone.CONTACT_ID + "=" + contactId;
Cursor cur = getContentResolver().query(Phone.CONTENT_URI, projection, selection, null, null); 
while (cur != null && cur.moveToNext()) {
    String number = cur.getString(0);
    int type = cur.getInt(1);        // home / office / personal
    String label = cur.getString(2); // a custom label in case type is "TYPE_CUSTOM"
    String labelStr = Phone.getTypeLabel(getResources(), type, label);

    Log.d(TAG, "got a phone: " + number + " (" + labelStr + ")");
}
if (cur != null) {
    cur.close();
}

DEBUG VERSION

String projection = new String[] { Phone._ID, Phone.CONTACT_ID, Phone.NUMBER, Phone.TYPE, Phone.LABEL };
String selection = Phone.CONTACT_ID + "=" + contactId;
Cursor cur = getContentResolver().query(Phone.CONTENT_URI, projection, selection, null, null); 
if (cur != null) {
    DatabaseUtils.dumpCursor(cur);
    cur.close();
}