从电话号码

时间:2018-01-16 07:16:41

标签: android android-contacts contact

假设我创建了一个空白名称(“”)并输入了2号

的联系人
  1. 1234
  2. 5678
  3. 现在我想从这个2号码中检索名字。如果name为空,我的代码总是将第一个数字作为名称。我怎样才能得到实名?

    我使用过这段代码:

        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:
    

2 个答案:

答案 0 :(得分:1)

你可以尝试

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

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

答案 1 :(得分:0)

我通过一些修改解决了这个问题。我添加了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的第一行开始。