Wednesday, March 4, 2015

Get all phone contacts in android

If you are looking to fetch all the phone contacts in android application or want to fecth google contact in android application similar to "whatsapp: than here is the method which will return all device contacts.

private void gettingPhoneContacts() {


   ContentResolver cr = getActivity().getContentResolver();
   // Read Contacts  
   Cursor c = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,     new String[] { ContactsContract.Contacts._ID,       ContactsContract.Contacts.DISPLAY_NAME,       ContactsContract.CommonDataKinds.Phone.NUMBER,       ContactsContract.RawContacts.ACCOUNT_TYPE },     ContactsContract.RawContacts.ACCOUNT_TYPE + " <> 'google' ",     null, null);
   if (c.getCount() <= 0) {    Toast.makeText(getActivity(), "No Phone Contact Found..!",                         Toast.LENGTH_SHORT).show();   }                else {               while (c.moveToNext()) {          String Phone_number = c       .getString(c         .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));          //Phone number
     String name = c       .getString(c         .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));                        //Name of contact
              }
   }    }  

5 comments:

  1. How to retrive email also??. Can u please show

    ReplyDelete
  2. Thanks Brian,
    I used above method to fetch the contacts.but I am getting duplicate contacts.
    can u please help ,How to avoid duplicate contacts?

    ReplyDelete
  3. What is getActivity() i am getting red and error

    ReplyDelete

Don't lose faith when you see others receive answers to their prayers

An elephant and a dog became pregnant at same time. Three months down the line the dog gave birth to six puppies. Six months later the dog...

 

G-Expo Template by Ipietoon Cute Blog Design