Skip to content

Instantly share code, notes, and snippets.

@flyfire
Last active August 6, 2019 03:26
Show Gist options
  • Select an option

  • Save flyfire/1f67b16f56a80a78ad0c6c63804a66fd to your computer and use it in GitHub Desktop.

Select an option

Save flyfire/1f67b16f56a80a78ad0c6c63804a66fd to your computer and use it in GitHub Desktop.
[Retrive Contacts name & phonenumber] #android #contacts
// https://stackoverflow.com/questions/12562151/android-get-all-contacts
private void getContactList() {
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
if ((cur != null ? cur.getCount() : 0) > 0) {
while (cur != null && cur.moveToNext()) {
String id = cur.getString(
cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(
ContactsContract.Contacts.DISPLAY_NAME));
if (cur.getInt(cur.getColumnIndex(
ContactsContract.Contacts.HAS_PHONE_NUMBER)) > 0) {
Cursor pCur = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
new String[]{id}, null);
while (pCur.moveToNext()) {
String phoneNo = pCur.getString(pCur.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.NUMBER));
Log.i(TAG, "Name: " + name);
Log.i(TAG, "Phone Number: " + phoneNo);
}
pCur.close();
}
}
}
if(cur!=null){
cur.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment