Android联系人排序字段汉字问题

以前读取联系人,按名字排序的字段是下面这个样子


ContactsContract.CommonDataKinds.Phone.SORT_KEY_PRIMARY

后来在6.0的手机上,读取的sort key,竟然是name的copy,比如读一个叫王二的联系人,正常的sort key 应该是“wang er”或者“W”,但6.0的系统读取出来还是"王二"

最后经过一番折腾发现应该这样写


if(android.os.Build.VERSION.SDK_INT>=19) {

PHONES_PROJECTION[PHONES_SORT_INDEX] ="phonebook_label";

}

也就是sort key的字段名字改了,附上一个小demo,内容很少直接贴代码

    private static final String[] PHONES_PROJECTION = new String[]{
            ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
            ContactsContract.CommonDataKinds.Phone.SORT_KEY_PRIMARY};
    
    private static final int PHONES_NUMBER_INDEX = 0;
    private static final int PHONES_SORT_INDEX = 1;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (android.os.Build.VERSION.SDK_INT >= 19) {
            PHONES_PROJECTION[PHONES_SORT_INDEX] = "phonebook_label";
        }
        Cursor cur = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                PHONES_PROJECTION, null, null, null);

        if (cur != null) {
            while (cur.moveToNext()) {
                String contactName = cur.getString(PHONES_NUMBER_INDEX);
                String sortKey = cur.getString(PHONES_SORT_INDEX);
                Log.i("Lucien_sort", "sortKey:" + sortKey + " contactName:" + contactName);
            }
        }
    }

你可能感兴趣的:(Android联系人排序字段汉字问题)