1

我想要包含 (FAMILY_NAME, GIVEN_NAME,MIDDLE_NAME,PHONETIC_FAMILY_NAME,PHONETIC_GIVEN_NAME,PHONETIC_MIDDLE_NAME,PREFIX,SUFFIX) 的联系人姓名。

我知道上述数据的列名以

android.provider.ContactsContract.CommonDataKinds.StructuredName

但我无法获取数据的URI

我正在使用 api 级别 8 的设备,所以我想使用来获取这些详细信息

android.provider.ContactsContract

我已经在社区中搜索过这个,但我无法得到想要的结果。

我为此工作了 4 个小时。

任何帮助将不胜感激。

我正在使用此代码

 Cursor cursor = activity.managedQuery(android.provider.ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
        if (cursor != null) 
        {
            if (cursor.moveToFirst()) 
            {

                int rows = cursor.getCount();
                int cols = cursor.getColumnCount();

                 do 
                    {

                         int _id = cursor.getInt(cursor.getColumnIndex("_id"));

                         int times_contacted = cursor.getInt(cursor.getColumnIndex("times_contacted"));
                         int has_phone_number = cursor.getInt(cursor.getColumnIndex("has_phone_number"));
                         int send_to_voicemail = cursor.getInt(cursor.getColumnIndex("send_to_voicemail"));
                         int starred = cursor.getInt(cursor.getColumnIndex("starred"));

// Here i want the contact names But i dont know what column name i have to pass to get them                      

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

提前致谢。

4

1 回答 1

2

好的,试试这个,让我知道会发生什么,

查看ContactsContract.CommonDataKinds.StructuredName类。您可以在那里找到您正在寻找的所有列。

   String whereName = ContactsContract.Data.MIMETYPE + " = ?";
    String[] whereNameParams = new String[] { ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE };
    Cursor nameCur = contentResolver.query(ContactsContract.Data.CONTENT_URI, null, whereName, whereNameParams, ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME);
    while (nameCur.moveToNext()) {
        String given = nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME));
        String family = nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME));
        String display = nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME));
    }
    nameCur.close();

看看这个 SO question How to get the firstname and lastname from android contacts?

编辑:查看使用 android 联系人的完整示例使用 Android 联系人,现在如果您想从任何联系人获取更多信息,请在该光标上添加特定列。有关更多列,请查看ContactsContract.CommonDataKinds

于 2011-12-02T07:40:41.820 回答