Click here to Skip to main content
15,902,938 members
Articles / Mobile Apps / Android
Tip/Trick

Get Phone Contact Details

Rate me:
Please Sign up or sign in to vote.
4.25/5 (8 votes)
18 Jun 2011CPOL 24.1K   12   5
Get Phone Contact Details
If you want to access the phone contacts, you can use the following code:

private void getMyPhoneNumber(){
        // Form an array specifying which columns to return.
        try{
       
        String[] projection = new String[] {
                                     People._ID,
                                     People.LABEL,//._COUNT,
                                     People.NAME,
                                     People.NUMBER,
                                     People.PRIMARY_EMAIL_ID
                                  };

        // Get the base URI for the People table in the Contacts content provider.
        Uri contacts =  People.CONTENT_URI;

        // Make the query.
        Cursor managedCursor = managedQuery(contacts,
                                projection, // Which columns to return
                                 null,       // Which rows to return (all rows)
                                 null,       // Selection arguments (none)
                                 // Put the results in ascending order by name
                                 People.NAME + " ASC");
       
        alertbox("Contacts", getColumnData(managedCursor));
        }
        catch(Exception e){
            alertbox("Exception", e.getMessage());
        }
       
    }

 private String getColumnData(Cursor cur){
        String ContactsName="";
        if (cur.moveToFirst()) {

            String name;
            String phoneNumber;
            String email;
            String Label;
            int nameColumn = cur.getColumnIndex(People.NAME);
            int phoneColumn = cur.getColumnIndex(People.NUMBER);
          
            String imagePath;
       
            do {
                // Get the field values
                name = cur.getString(nameColumn);
                phoneNumber = cur.getString(phoneColumn);
                 email =cur.getString(cur.getColumnIndex(People.PRIMARY_EMAIL_ID));
                 Label =cur.getString(cur.getColumnIndex(People.LABEL));
                ContactsName+="name:"+name+" Phoneno:"+phoneNumber+"email:"+email+"label:"+Label;
              
            } while (cur.moveToNext());

        }
        return ContactsName;
    }

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Sapple Systems Ptv. Ltd India
India India
My Self Atul Yadav and have exp. in Application Developer in a wide variety of business applications. Particularly interested in client/server and relational database design using MS-SQL Server.

Specialties:
Microsoft Certified Professional
Asp.net,asp classic,C#, xml ,javascript,Flex action script,
SQL,Smart phone Applications.

My Blogs:
Flex Blog: Flex.co.nr
Android Blog: Android Beginners
Dot Net Blog: Dot Net Debugger

Comments and Discussions

 
GeneralMy vote of 3 Pin
Dheeraj Singh Bhadoria19-Mar-13 4:17
professionalDheeraj Singh Bhadoria19-Mar-13 4:17 
GeneralThis is deprecated, use ContactsContract. Pin
John Harlacker7-Jan-12 7:24
John Harlacker7-Jan-12 7:24 
GeneralReason for my vote of 5 i think its easy to understand Pin
komal 220-Dec-11 9:18
komal 220-Dec-11 9:18 
Generalwhere do i get the variable People? Pin
Gonzalo de Cordoba30-Oct-11 2:55
Gonzalo de Cordoba30-Oct-11 2:55 
GeneralReason for my vote of 2 Not really an article, just two meth... Pin
ShermansLagoon12-Sep-11 8:13
ShermansLagoon12-Sep-11 8:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.