Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read all contacts' phone numbers in android

I'm using this code to retrieve all contact names and phone numbers:

String[] projection = new String[] {     People.NAME,     People.NUMBER };  Cursor c = ctx.getContentResolver().query(People.CONTENT_URI, projection, null, null, People.NAME + " ASC"); c.moveToFirst();  int nameCol = c.getColumnIndex(People.NAME); int numCol = c.getColumnIndex(People.NUMBER);  int nContacts = c.getCount();  do {   // Do something } while(c.moveToNext()); 

However, this will only return the primary number for each contact, but I want to get the secondary numbers as well. How can i do this?

like image 406
shuwo Avatar asked Mar 01 '10 13:03

shuwo


2 Answers

Following code shows an easy way to read all phone numbers and names:

Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null); while (phones.moveToNext()) {   String name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));   String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));  } phones.close(); 

NOTE: getContentResolver is a method from the Activity context.

like image 157
Dinesh Kumar Avatar answered Sep 23 '22 05:09

Dinesh Kumar


You can read all of the telephone numbers associated with a contact in the following manner:

Uri personUri = ContentUris.withAppendedId(People.CONTENT_URI, personId); Uri phonesUri = Uri.withAppendedPath(personUri, People.Phones.CONTENT_DIRECTORY); String[] proj = new String[] {Phones._ID, Phones.TYPE, Phones.NUMBER, Phones.LABEL} Cursor cursor = contentResolver.query(phonesUri, proj, null, null, null); 

Please note that this example (like yours) uses the deprecated contacts API. From eclair onwards this has been replaced with the ContactsContract API.

like image 29
Graeme Duncan Avatar answered Sep 19 '22 05:09

Graeme Duncan