Thursday, January 28, 2010

Get Phone Contact Details

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;
    }

No comments:

Post a Comment