Showing posts with label Read Contact from Contact Book. Show all posts
Showing posts with label Read Contact from Contact Book. Show all posts

Tuesday, August 12, 2014

Read Contact from Contact Book

  • Add uses-permission in AndroidManifest.xml


 <uses-permission android:name="android.permission.READ_CONTACTS" />


  • Create Contacts.java


public class Contacts {
       String contactName = "";
       String contactNumber = "";

       public String getContactName() {
             return contactName;
       }

       public void setContactName(String contactName) {
             this.contactName = contactName;
       }

       public String getContactNumber() {
             return contactNumber;
       }

       public void setContactNumber(String contactNumber) {
             this.contactNumber = contactNumber;
       }

}


  • Add Method getContactList() in Activity 

import android.provider.ContactsContract.Data;

private List<Contacts> getContactList() {
             ArrayList<Contacts> contactList = new ArrayList<Contacts>();

             @SuppressWarnings("unused")
             Uri contactUri = ContactsContract.Contacts.CONTENT_URI;
             String[] PROJECTION = new String[] { ContactsContract.Contacts._ID,
                           ContactsContract.Contacts.DISPLAY_NAME,
                           ContactsContract.Contacts.HAS_PHONE_NUMBER, };
             String SELECTION = ContactsContract.Contacts.HAS_PHONE_NUMBER + "='1'";
             Cursor cursorContacts = getContentResolver().query(
                           ContactsContract.Contacts.CONTENT_URI, PROJECTION, SELECTION,
                           null, null);

             if (cursorContacts.getCount() > 0) {
                    while (cursorContacts.moveToNext()) {
                           Contacts mContact = new Contacts();

                           int nameFieldColumnIndex = 0;
                           int numberFieldColumnIndex = 0;

                           String contactId = cursorContacts.getString(cursorContacts
                                        .getColumnIndex(ContactsContract.Contacts._ID));

                           nameFieldColumnIndex = cursorContacts
                                        .getColumnIndex(PhoneLookup.DISPLAY_NAME);
                           if (nameFieldColumnIndex > -1) {
                                 mContact.setContactName(cursorContacts
                                               .getString(nameFieldColumnIndex));
                           }

                           PROJECTION = new String[] { Phone.NUMBER };
                           final Cursor phone = managedQuery(Phone.CONTENT_URI,
                                        PROJECTION, Data.CONTACT_ID + "=?",
                                        new String[] { String.valueOf(contactId) }, null);

                           if (phone.moveToFirst()) {
                                 while (!phone.isAfterLast()) {
                                        numberFieldColumnIndex = phone
                                                      .getColumnIndex(Phone.NUMBER);
                                        if (numberFieldColumnIndex > -1) {
                                               mContact.setContactNumber(phone
                                                            .getString(numberFieldColumnIndex));
                                               phone.moveToNext();
                                               contactList.add(mContact);
                                        }
                                 }
                           }
                           phone.close();
                    }
                    cursorContacts.close();
             }
             return contactList;
       }