从 phone 号码获取实际联系人姓名

Get actual Contact name from phone number

假设我创建了一个名称为空 ("") 的联系人并输入了 2 个号码

  1. 1234
  2. 5678

现在我想从这 2 个号码中检索姓名。如果名称为空,我的代码总是将第一个数字作为名称。我怎样才能得到真实姓名?

我用过这个代码:

    public static String getContactNamefromNumber(Context context,
        String contactNumber) {
    if (contactNumber == null || contactNumber.equals(""))
        return null;
    String name = null;

    Cursor contactLookupCursor = context.getContentResolver().query(
            Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
                    Uri.encode(contactNumber)),
            new String[] { PhoneLookup.DISPLAY_NAME, PhoneLookup._ID},
            null, null, null);
    if (contactLookupCursor != null) {

        if (contactLookupCursor.moveToFirst()) {
            do {
                name = contactLookupCursor.getString(contactLookupCursor
                        .getColumnIndexOrThrow(PhoneLookup.DISPLAY_NAME));
            } while (contactLookupCursor.moveToNext());
        }
        contactLookupCursor.close();
    }

    return name;
}

如果实际联系人姓名为空,此代码总是给我第一个数字作为姓名。我想要实际名称(它可能是空白或有效名称)而不是数字。是否有办法确定名称为空?这也将解决我的问题。

已添加: 我也试过下面的代码:

    String name ="";
    Cursor nameCur = context.getContentResolver()
            .query(ContactsContract.Data.CONTENT_URI,
                    null, 
                    ContactsContract.CommonDataKinds.StructuredName.CONTACT_ID + " = ?",
                    new String[] {
                    Long.valueOf(contactId).toString()  }, null);
    if (nameCur != null && nameCur.getCount() > 0 ) {

        nameCur.moveToFirst();
        do {
            name += "" + nameCur.getPosition() + " : ";
            String displayName = nameCur.getString(nameCur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            if (displayName == null) displayName = "";

            String firstName  = nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME));
            if (firstName == null) firstName = "";

            String middleName = nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME));
            if (middleName == null) middleName = "";

            String lastName = nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME));
            if (lastName == null) lastName = "";

            name +=  "First: "  + firstName + " Middle: " + middleName + " Last: " + lastName +  "\n";

        }while (nameCur.moveToNext());
        nameCur.close();
    }

我在这里使用 contactid 来加载姓名,但不适用于所有联系人。对于某些联系人 nameCur 包含不止一行(2,3,4,...8 等)。有时第一行包含空白信息。这是我从 nameCur 获得的示例信息。对于下面的情况,我得到了尺寸为 9 的 nameCur

0 : First:  Middle:  Last:
1 : First: Test_Name Middle:  Last: Deb
2 : First:  Middle:  Last:
3 : First:  Middle:  Last:
4 : First:  Middle:  Last:
5 : First: 3 Middle:  Last:
6 : First: com.google Middle:  Last:
7 : First:  Middle:  Last:
8 : First: 2 Middle:  Last:

你可以试试

String given = nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME));

String family = nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME));

我已经通过一些修改解决了这个问题。我添加了 where 子句

ContactsContract.Data.MIMETYPE = ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE

和排序顺序

ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME

这是我使用的代码:

    public static String getActualContactNameByContactId(Context context, long contactId){

    String name ="";

    String whereName =   ContactsContract.CommonDataKinds.StructuredName.CONTACT_ID + " = ? " + " AND " + ContactsContract.Data.MIMETYPE + " = ?";
    String[] whereNameParams = new String[] { String.valueOf(contactId), ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE };
    String sortOrder = ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME;

    // get firstName & lastName
    Cursor nameCur = context.getContentResolver()
            .query(ContactsContract.Data.CONTENT_URI,
                    null,
                    whereName,
                    whereNameParams,
                    sortOrder);
    if (nameCur != null && nameCur.getCount() > 0 ) {

        nameCur.moveToFirst();

        try {           

         nameCur.getString(nameCur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
        //  if (displayName == null) displayName = "";

            String firstName  = nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME));
            if (firstName == null) firstName = "";
            //Log.d("--> ", firstName.length()>0?firstName:displayName);

            String middleName = nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME));
            if (middleName == null) middleName = "";

            String lastName = nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME));
            if (lastName == null) lastName = "";

            name = firstName.trim();


            if (middleName.trim().length() > 0) {
                if (name.length() > 0) {
                    name+= " ";
                }

                name+= middleName.trim();
            }

            if (lastName.trim().length() > 0) {
                if (name.length() > 0) {
                    name+= " ";
                }

                name+= lastName.trim();
            }



        }catch (Exception e){

        }finally {
            nameCur.close();
        }


    }


    return name;

}

我只取了 cursor 的第一行,因为多行可能 return。