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

时间:2021-02-10 18:11:46

Suppose I have created a Contact with blank name ("") and entered 2 number

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

  1. 1234
  2. 1234
  3. 5678
  4. 5678

Now I want to retrieve name from this 2 number. My code always gets first number as name if name is empty. How can I get actual name?

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

I have used this code:

我用过这段代码:

    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;
}

This code always give me first number as name if actual contact name is blank. I want the actual name( it may blank or valid name) not number. If there is anyway to determine name is blank ? this will also solve my problem.

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

Added: I also tried below code:

补充:我也试过下面的代码:

    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();
    }

Here i have used contactid to load name but not working for all contacts. For some contacts nameCur contain more than one rows(2,3,4,...8 etc). And sometime first row contains blank info . Here is a sample info i get form nameCur. For below case i got nameCur with size 9.

在这里,我使用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 个解决方案

#1


1  

You can try

你可以试试

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

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

#2


0  

I have solved this with some modification. I have added where clause

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

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

and sort order

和排序顺序

ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME

Here is the code i have used:

这是我用过的代码:

    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;

}

I have taken only the first row form the cursor as multiple row may return.

我只从光标的第一行开始,因为多行可能会返回。

#1


1  

You can try

你可以试试

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

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

#2


0  

I have solved this with some modification. I have added where clause

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

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

and sort order

和排序顺序

ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME

Here is the code i have used:

这是我用过的代码:

    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;

}

I have taken only the first row form the cursor as multiple row may return.

我只从光标的第一行开始,因为多行可能会返回。