How to retrieve Contact name and phone number in Android

后端 未结 6 1258
旧时难觅i
旧时难觅i 2020-12-10 05:56

I\'m trying to retrieve contact list with there name and phone numbers. I try following code:

 // Get a cursor over every contact.
    Cursor cursor = getCon         


        
6条回答
  •  攒了一身酷
    2020-12-10 06:44

    Look on the sample code for retrieve the contacts from android mobile,

        Cursor cursor = context.getContentResolver().query(
                    ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
    
    
    String contactId = cursor.getString(cursor
                        .getColumnIndex(ContactsContract.Contacts._ID));
    
    String name = cursor.getString(cursor                   .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
    
                Cursor phones = context.getContentResolver().query(
                        Phone.CONTENT_URI, null,
                        Phone.CONTACT_ID + " = " + contactId, null, null);
                while (phones.moveToNext()) {
                    String number = phones.getString(phones
                            .getColumnIndex(Phone.NUMBER));
                    int type = phones.getInt(phones.getColumnIndex(Phone.TYPE));
                    switch (type) {
                    case Phone.TYPE_HOME:                   
                         Log.i("TYPE_HOME", "" + number);
                        break;
                    case Phone.TYPE_MOBILE:                 
                        Log.i("TYPE_MOBILE", "" + number);
                        break;
                    case Phone.TYPE_WORK:                   
                         Log.i("TYPE_WORK", "" + number);
                        break;
                    case Phone.TYPE_FAX_WORK:                   
                        Log.i("TYPE_FAX_WORK", "" + number);
                        break;
                    case Phone.TYPE_FAX_HOME:
                        Log.i("TYPE_FAX_HOME", "" + number);
                        break;
    
                    case Phone.TYPE_OTHER:
                        Log.i("TYPE_OTHER", "" + number);
                        break;
                    }
                }
                phones.close();
    cursor.close();
    

提交回复
热议问题