how to retrieve the list of contacts in android

前端 未结 2 1394
滥情空心
滥情空心 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<String, String> getAddressBook(Context context)
    {
        Map<String, String> result = new HashMap<String, String>();
        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

    <uses-permission android:name="android.permission.READ_CONTACTS"/>
    
    0 讨论(0)
  • 2021-01-06 12:33

    You can use the below query to retrieve the favourite contacts from Contacts DB

    Cursor cursor = this.managedQuery(ContactsContract.Contacts.CONTENT_URI, projection, "starred=?",new String[] {"1"}, null);
    
    0 讨论(0)
提交回复
热议问题