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
}
} }
really helpful
ReplyDeleteThanks Brian..
ReplyDeleteHow to retrive email also??. Can u please show
ReplyDeleteThanks Brian,
ReplyDeleteI used above method to fetch the contacts.but I am getting duplicate contacts.
can u please help ,How to avoid duplicate contacts?
What is getActivity() i am getting red and error
ReplyDelete