Read all contact phone numbers in Android

I use this code to extract 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 result in a return of the primary number for each contact, but I also want to get secondary numbers. How can i do this?

+68
android phone-number contacts android-contacts
Mar 01 '10 at 13:31
source share
13 answers

You can read all the phone numbers associated with a contact as follows:

 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: this example (for example, yours) uses the outdated contacts API. Starting with eclair, it has been replaced with the ContactsContract API.

+29
Mar 01
source share

The 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 an Activity context.

+144
Jan 24 '12 at 12:16
source share

This code shows how to get all phone numbers for each contact.

 ContentResolver cr = getActivity().getContentResolver(); Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); if (cur.getCount() > 0) { while (cur.moveToNext()) { String id = cur.getString(cur.getColumnIndex( ContactsContract.Contacts._ID)); String name = cur.getString(cur.getColumnIndex( ContactsContract.Contacts.DISPLAY_NAME)); if (Integer.parseInt(cur.getString(cur.getColumnIndex( ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) { Cursor pCur = cr.query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", new String[]{id}, null); while (pCur.moveToNext()) { int phoneType = pCur.getInt(pCur.getColumnIndex( ContactsContract.CommonDataKinds.Phone.TYPE)); String phoneNumber = pCur.getString(pCur.getColumnIndex( ContactsContract.CommonDataKinds.Phone.NUMBER)); switch (phoneType) { case Phone.TYPE_MOBILE: Log.e(name + "(mobile number)", phoneNumber); break; case Phone.TYPE_HOME: Log.e(name + "(home number)", phoneNumber); break; case Phone.TYPE_WORK: Log.e(name + "(work number)", phoneNumber); break; case Phone.TYPE_OTHER: Log.e(name + "(other number)", phoneNumber); break; default: break; } } pCur.close(); } } } 
+24
Oct 24 '13 at
source share

In case this helps, I have an example that uses the ContactsContract API to first find a contact by name, then iterates through the details that look for specific types of numbers:

How to use ContactsContract to retrieve phone numbers and email addresses

+14
Mar 01 '10 at 17:45
source share
 String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID)); String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) { System.out.println("name : " + name + ", ID : " + id); // get the <span id="IL_AD4" class="IL_AD">phone // number</span> Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { id }, null); while (pCur.moveToNext()) { String phone = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); System.out.println("phone" + phone); } pCur.close(); 
+10
Aug 30 2018-12-12T00:
source share

You can get all the phone contacts using this:

  Cursor c = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, new String[] { ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.RawContacts.ACCOUNT_TYPE }, ContactsContract.RawContacts.ACCOUNT_TYPE + " <> 'google' ", null, null); 

check out the full example HERE ...........

+9
Mar 04 '15 at 5:37
source share

Likewise, simply get your other numbers using other People links.

 People.TYPE_HOME People.TYPE_MOBILE People.TYPE_OTHER People.TYPE_WORK 
+1
Mar 01
source share

This one finally gave me the answer I was looking for. It allows you to get all the names and phone numbers of contacts on the phone.

 package com.test; import android.app.Activity; import android.content.ContentResolver; import android.database.Cursor; import android.os.Bundle; import android.provider.ContactsContract; public class TestContacts extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ContentResolver cr = getContentResolver(); Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); if (cur != null && cur.getCount() > 0) { while (cur.moveToNext()) { if (Integer.parseInt.equals(cur.getString(cur.getColumnIndex( ContactsContract.Contacts.HAS_PHONE_NUMBER)))) { String id = cur.getString(cur.getColumnIndex( ContactsContract.Contacts._ID)); String name = cur.getString(cur.getColumnIndex( ContactsContract.Contacts.DISPLAY_NAME)); Cursor pCur = cr.query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { id }, null); int i = 0; int pCount = pCur.getCount(); String[] phoneNum = new String[pCount]; String[] phoneType = new String[pCount]; while (pCur != null && pCur.moveToNext()) { phoneNum[i] = pCur.getString(pCur.getColumnIndex( ContactsContract.CommonDataKinds.Phone.NUMBER)); phoneType[i] = pCur.getString(pCur.getColumnIndex( ContactsContract.CommonDataKinds.Phone.TYPE)); i++; } } } } } } 
+1
Jul 19 '14 at 19:37
source share

I corrected my need

Manifest permissions

  <uses-permission android:name="android.permission.READ_CONTACTS" /> <uses-permission android:name="android.permission.WRITE_CONTACTS"/> <!-- Read Contacts from phone --> <uses-permission android:name="android.permission.read_contacts" /> <uses-permission android:name="android.permission.read_phone_state" /> <uses-permission android:name="android.permission.READ_CALL_LOG" /> 

Note. After granting permissions in the manifest, some mobile phones may deny, if you need to go to Settings โ†’ Applications โ†’ find an application, click on it and Enable the necessary permissions

enter image description here

  btn_readallcontacks.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null); List<User> userList = new ArrayList<User>(); while (phones.moveToNext()) { User user = new User(); user.setNamee(phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME))); user.setPhone(phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER))); userList.add(user); Log.d(TAG, "Name : " + user.getNamee().toString()); Log.d(TAG, "Phone : " + user.getPhone().toString()); } phones.close(); } }); 
0
Aug 08 '16 at 10:30
source share

Code for getting the name of the contact (ascending) and number

Additional link: show the contact in the watch list

 Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME+" ASC"); 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(); 
0
May 12 '17 at 11:21
source share

Downloading contacts in the background using CursorLoader:

  CursorLoader cursor = new CursorLoader(this, ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null); Cursor managedCursor = cursor.loadInBackground(); int number = managedCursor.getColumnIndex(ContactsContract.Contacts.Data.DATA1); int name = managedCursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME); int index = 0; while (managedCursor.moveToNext()) { String phNumber = managedCursor.getString(number); String phName = managedCursor.getString(name); } 
0
Oct 03 '18 at 8:57
source share

The accepted answer works, but no unique number is specified.

See this code, it returns unique numbers.

 public static void readContacts(Context context) { if (context == null) return; ContentResolver contentResolver = context.getContentResolver(); if (contentResolver == null) return; String[] fieldListProjection = { ContactsContract.CommonDataKinds.Phone.CONTACT_ID, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone.NORMALIZED_NUMBER, ContactsContract.Contacts.HAS_PHONE_NUMBER }; Cursor phones = contentResolver .query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI , fieldListProjection, null, null, null); HashSet<String> normalizedNumbersAlreadyFound = new HashSet<>(); if (phones != null && phones.getCount() > 0) { while (phones.moveToNext()) { String normalizedNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NORMALIZED_NUMBER)); if (Integer.parseInt(phones.getString(phones.getColumnIndex( ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) { if (normalizedNumbersAlreadyFound.add(normalizedNumber)) { String id = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID)); String name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); Log.d("test", " Print all values"); } } } phones.close(); } } 
0
Dec 29 '18 at 13:55
source share
  package com.example.readcontacts; import java.util.ArrayList; import android.app.Activity; import android.app.ProgressDialog; import android.content.ContentResolver; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.os.Handler; import android.provider.ContactsContract; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.Toast; public class MainActivity extends Activity { private ListView mListView; private ProgressDialog pDialog; private Handler updateBarHandler; ArrayList<String> contactList; Cursor cursor; int counter; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); pDialog = new ProgressDialog(this); pDialog.setMessage("Reading contacts..."); pDialog.setCancelable(false); pDialog.show(); mListView = (ListView) findViewById(R.id.list); updateBarHandler =new Handler(); // Since reading contacts takes more time, let run it on a separate thread. new Thread(new Runnable() { @Override public void run() { getContacts(); } }).start(); // Set onclicklistener to the list item. mListView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { //TODO Do whatever you want with the list data Toast.makeText(getApplicationContext(), "item clicked : \n"+contactList.get(position), Toast.LENGTH_SHORT).show(); } }); } public void getContacts() { contactList = new ArrayList<String>(); String phoneNumber = null; String email = null; Uri CONTENT_URI = ContactsContract.Contacts.CONTENT_URI; String _ID = ContactsContract.Contacts._ID; String DISPLAY_NAME = ContactsContract.Contacts.DISPLAY_NAME; String HAS_PHONE_NUMBER = ContactsContract.Contacts.HAS_PHONE_NUMBER; Uri PhoneCONTENT_URI = ContactsContract.CommonDataKinds.Phone.CONTENT_URI; String Phone_CONTACT_ID = ContactsContract.CommonDataKinds.Phone.CONTACT_ID; String NUMBER = ContactsContract.CommonDataKinds.Phone.NUMBER; Uri EmailCONTENT_URI = ContactsContract.CommonDataKinds.Email.CONTENT_URI; String EmailCONTACT_ID = ContactsContract.CommonDataKinds.Email.CONTACT_ID; String DATA = ContactsContract.CommonDataKinds.Email.DATA; StringBuffer output; ContentResolver contentResolver = getContentResolver(); cursor = contentResolver.query(CONTENT_URI, null,null, null, null); // Iterate every contact in the phone if (cursor.getCount() > 0) { counter = 0; while (cursor.moveToNext()) { output = new StringBuffer(); // Update the progress message updateBarHandler.post(new Runnable() { public void run() { pDialog.setMessage("Reading contacts : "+ counter++ +"/"+cursor.getCount()); } }); String contact_id = cursor.getString(cursor.getColumnIndex( _ID )); String name = cursor.getString(cursor.getColumnIndex( DISPLAY_NAME )); int hasPhoneNumber = Integer.parseInt(cursor.getString(cursor.getColumnIndex( HAS_PHONE_NUMBER ))); if (hasPhoneNumber > 0) { output.append("\n First Name:" + name); //This is to read multiple phone numbers associated with the same contact Cursor phoneCursor = contentResolver.query(PhoneCONTENT_URI, null, Phone_CONTACT_ID + " = ?", new String[] { contact_id }, null); while (phoneCursor.moveToNext()) { phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(NUMBER)); output.append("\n Phone number:" + phoneNumber); } phoneCursor.close(); // Read every email id associated with the contact Cursor emailCursor = contentResolver.query(EmailCONTENT_URI, null, EmailCONTACT_ID+ " = ?", new String[] { contact_id }, null); while (emailCursor.moveToNext()) { email = emailCursor.getString(emailCursor.getColumnIndex(DATA)); output.append("\n Email:" + email); } emailCursor.close(); } // Add the contact to the ArrayList contactList.add(output.toString()); } // ListView has to be updated using a ui thread runOnUiThread(new Runnable() { @Override public void run() { ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.list_item, R.id.text1, contactList); mListView.setAdapter(adapter); } }); // Dismiss the progressbar after 500 millisecondds updateBarHandler.postDelayed(new Runnable() { @Override public void run() { pDialog.cancel(); } }, 500); } } } List item 
-one
Jul 06 '16 at 8:45
source share



All Articles