Hi all..

In today’s post I will show you how to get all information from the ANDROID Contacts programatically.
Just copy and paste this code to your mail java file and check the Logcat for output.

package pack.coderzheaven;

import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;

public class GetContactsDemo extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        readContacts();
    }

    public void readContacts(){
    	 ContentResolver cr = 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) {
		        	System.out.println("name : " + name + ", ID : " + id);

		        	// get the phone number
		        	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();


         	        // get email and type

         	       Cursor emailCur = cr.query(
         	    			ContactsContract.CommonDataKinds.Email.CONTENT_URI,
         	    			null,
         	    			ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
         	    			new String[]{id}, null);
     	    		while (emailCur.moveToNext()) {
     	    		    // This would allow you get several email addresses
     	    	            // if the email addresses were stored in an array
     	    		    String email = emailCur.getString(
     	    	                      emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
     	    	 	    String emailType = emailCur.getString(
     	    	                      emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));

     	    		  System.out.println("Email " + email + " Email Type : " + emailType);
         	    	}
         	    	emailCur.close();

         	    	// Get note.......
         	    	String noteWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
         	        String[] noteWhereParams = new String[]{id,
         	 		ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE};
         	                Cursor noteCur = cr.query(ContactsContract.Data.CONTENT_URI, null, noteWhere, noteWhereParams, null);
	         	 	if (noteCur.moveToFirst()) {
	         	 	    String note = noteCur.getString(noteCur.getColumnIndex(ContactsContract.CommonDataKinds.Note.NOTE));
	         	 	  System.out.println("Note " + note);
	         	 	}
	         	 	noteCur.close();

	         	 	//Get Postal Address....

	         	 	String addrWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
	         		String[] addrWhereParams = new String[]{id,
	         			ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE};
	         		Cursor addrCur = cr.query(ContactsContract.Data.CONTENT_URI,
	         	                null, null, null, null);
	         		while(addrCur.moveToNext()) {
	         			String poBox = addrCur.getString(
	         	                     addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POBOX));
	         	 		String street = addrCur.getString(
	         	                     addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.STREET));
	         	 		String city = addrCur.getString(
	         	                     addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.CITY));
	         	 		String state = addrCur.getString(
	         	                     addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.REGION));
	         	 		String postalCode = addrCur.getString(
	         	                     addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE));
	         	 		String country = addrCur.getString(
	         	                     addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY));
	         	 		String type = addrCur.getString(
	         	                     addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.TYPE));

	         	 		// Do something with these....

	         	 	}
	         	 	addrCur.close();

	         	 	// Get Instant Messenger.........
	         	 	String imWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
	         	 	String[] imWhereParams = new String[]{id,
	         	 	    ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE};
	         	 	Cursor imCur = cr.query(ContactsContract.Data.CONTENT_URI,
	         	            null, imWhere, imWhereParams, null);
	         	 	if (imCur.moveToFirst()) {
	         	 	    String imName = imCur.getString(
	         	                 imCur.getColumnIndex(ContactsContract.CommonDataKinds.Im.DATA));
	         	 	    String imType;
	         	 	    imType = imCur.getString(
	         	                 imCur.getColumnIndex(ContactsContract.CommonDataKinds.Im.TYPE));
	         	 	}
	         	 	imCur.close();

	         	 	// Get Organizations.........

	         	 	String orgWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
	         	 	String[] orgWhereParams = new String[]{id,
	         	 		ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE};
	         	 	Cursor orgCur = cr.query(ContactsContract.Data.CONTENT_URI,
	         	                null, orgWhere, orgWhereParams, null);
	         	 	if (orgCur.moveToFirst()) {
	         	 		String orgName = orgCur.getString(orgCur.getColumnIndex(ContactsContract.CommonDataKinds.Organization.DATA));
	         	 		String title = orgCur.getString(orgCur.getColumnIndex(ContactsContract.CommonDataKinds.Organization.TITLE));
	         	 	}
	         	 	orgCur.close();
	 	        }
	        }
 	   }
    }

}

Note : Make sure you add this permission to the AndroidManifest File.

Download the complete android java source code from the link below.

coderzheaven.com/uploads/GetContactsTwo.rar

Posts to come ……..

How to add and delete contacts programatically. keep visiting coderzheaven….

Some of the posts you want to see are located here
Please leave your valuable comments on this post.

 

29 Comments

  1. Anonymous July 20, 2011 at 12:09 pm

    The ONLY resource that worked. I’ve been Googling this problem for days. Most useful and accurate “tutorial”, if you will.

    Thank you.

     
  2. Prince_Alvin October 31, 2011 at 4:11 pm

    thanks a lot man works like a charm….
    and easy to understand the coding……
    🙂

     
  3. TechnoTalkative November 15, 2011 at 3:36 pm

    Facing difficulty while i was copying this code for testing. Can you please post the downloadable example.

     
    • James November 15, 2011 at 11:01 pm

      Hi paresh, I have added the source code below. please check.

       
  4. Aki143S December 16, 2011 at 1:50 am

    Thanks a lot sir. Its a very detailed and accurate tutorial

     
  5. Ravindra December 19, 2011 at 1:29 pm

    Good job dear.

     
  6. dheeraj December 23, 2011 at 3:29 pm

    hi,I am using your code to get contacts of android.i am taking the contact no.and name but it works very slow on real device.the screen turns black and then shows the contact after a while.
    what is the problem.please help me

     
  7. manisha January 2, 2012 at 11:36 am

    how to get images from contact list????,
    please tell me…..

     
    • James January 2, 2012 at 10:31 pm

      OK Manish I will check that and reply.

       
  8. dleviathan March 10, 2012 at 8:30 am

    But with this code u program run very slowly i had run 1 contacts with 348 contact not full details, this code take > 1m. How to improve effective? I need idea! thanks.

     
  9. oahdshai June 6, 2012 at 9:31 pm

    see here for more details how to improve performance:
    http://stackoverflow.com/questions/10921109/get-contacts-performance-issue

     
    • James June 7, 2012 at 3:13 pm

      oahdshai Thanks for the information.

       
  10. Lance June 12, 2012 at 6:07 pm

    how do you get the contact number?

     
    • James June 13, 2012 at 5:20 pm

      Hi Lance :-
      Check this line
      String phone = pCur.getString( pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
      System.out.println(“phone” + phone); // printing the phone number-> check Logcat

       
  11. Guri August 14, 2012 at 11:58 am

    great tutorial dude !!!!!

     
  12. emran February 13, 2013 at 8:38 am

    Good job dear.

     
  13. Benild March 13, 2013 at 7:07 pm

    great tutorial…..
    I am getting all contact from the phone, after that i display only number to textview, first time i pick the contact and can display to the textview.then getting another contact, but it cannot be change the previous selected one. always show the first selected contact.
    Plz help me, Thanks in advance……….

     
  14. Rishika December 20, 2013 at 7:18 am

    But how to show all contacts with all details in List ??

    please help

     
    • James December 25, 2013 at 6:43 am

      You could just create an adapter for the listview with the widgets you want and assign values using the adapter.

       
  15. praveen March 19, 2014 at 7:30 pm

    hi myself praveen

    i lost my mobile last week i need the all contacts # when i was using WIFI the contacts will synchronization to mail. There is any way is there to get my contacts through mail id

    Please contact to mail : Praveensbi4@gmail.com

    contact : 9916594114

    Regards
    Praveen.G

     
  16. Saad Sheikh July 16, 2014 at 7:25 pm

    Its really awesome…

     
  17. Govind November 10, 2014 at 8:24 am

    Thanks Man, For great tutorial. Could you guide me to Get and Add Relationship From and to contacts ?

    Thanks again.

     
  18. habib July 17, 2015 at 2:57 am

    hi.
    my language is not English.
    I want to learn android program.
    I need cods that send contacts to email.
    can you help me?
    thank you.

     
  19. SWAPNIL CHAUHAN August 31, 2015 at 11:05 am

    Hi James
    I am not able to get the list. The app is running smooth but not displaying any contacts.
    What to do?
    Thanks

     
  20. Nandish patil December 3, 2017 at 10:04 am

    Namaskar ,
    Nice code works well
    thanks
    Nandish Patil

     
  21. Pingback: Come caricare tutti i contatti con il tempo minimo in Android IL ANDORID

  22. Pingback: Como cairregair todos os contatos com tempo mínimo no Android Android Cake

  23. Dena April 3, 2018 at 7:18 am

    Hmm it seems like your blog ate my first comment
    (it was super long) so I guess I’ll just sum it up what I
    had written and say, I’m thoroughly enjoying your blog.
    I too am an aspiring blog writer but I’m still new to everything.

    Do you have any points for newbie blog writers? I’d definitely appreciate it.

     
  24. himanshu April 19, 2018 at 6:39 am

    hello i really liked your tutorial, you almost provide everything from contact but i need one more thing i.e event like birth day and website.
    Thanks in advance. liked

     

Leave a Reply

Your email address will not be published. Required fields are marked *

 

 

 

Theme by HermesThemes

Copyright © 2018 CoderzHeaven. All Rights Reserved

Please wait...

Subscribe to our newsletter

Want to be notified when our article is published? Enter your email address and name below to be the first to know.