how to retrieve the list of contacts in android

前端 未结 2 1418
滥情空心
滥情空心 2021-01-06 11:58

my purpose to retrieve the list of contacts (name, number) is recorded in a json object, to send via web service to the server

then I found the code to visit contact

2条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-06 12:25

    here my code

    public static Map getAddressBook(Context context)
    {
        Map result = new HashMap();
        Cursor cursor = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
        while(cursor.moveToNext()) 
        {
            int phone_idx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
            int name_idx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
            String phone = cursor.getString(phone_idx);
            String name = cursor.getString(name_idx);
            result.put(name, phone);
        }
        cursor.close();
    
        return result;
    }
    

    and need

    
    

提交回复
热议问题