Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Mobile / Android

Get Phone Contact Details

4.25/5 (8 votes)
18 Jun 2011CPOL 25K  
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)