Android contacts Display Name and Phone Number(s) in single database query?

前端 未结 3 1704
被撕碎了的回忆
被撕碎了的回忆 2020-11-27 11:12

I\'m trying to obtain a list of contacts from the native database with their Display Name and Phone Number (any or all). There are many methods for obtaining this informatio

3条回答
  •  萌比男神i
    2020-11-27 11:34

    Try this code:

    Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
    String[] projection    = new String[] {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
                    ContactsContract.CommonDataKinds.Phone.NUMBER};
    
    Cursor people = getContentResolver().query(uri, projection, null, null, null);
    
    int indexName = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
    int indexNumber = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
    
    if(people.moveToFirst()) {
        do {
            String name   = people.getString(indexName);
            String number = people.getString(indexNumber);
            // Do work...
        } while (people.moveToNext());
    }
    

提交回复
热议问题