0

Is it possible to get contacts that are displayed at official contacts application?

I tried to retrieve contacts in this way:

    contentResolver = ApplicationSingleton.getInstance().getContentResolver();
    String[] projection = new String[]{ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME, ContactsContract.Contacts.HAS_PHONE_NUMBER, ContactsContract.Contacts.STARRED};
    String selection = null;
    Cursor cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, projection, selection, null, null);


    while (cursor.moveToNext()) {
        String name = (cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)));
        Log.d("CONTACT: ", name);
    }

    cursor.close();

but then I get all contacts which are connected to my email.

Thanks.

Illia Vlasov
  • 251
  • 4
  • 10
  • possible duplicate of [Android How to read android Contacts and SIM Contacts?](http://stackoverflow.com/questions/13575286/android-how-to-read-android-contacts-and-sim-contacts) – ByteWelder Jul 16 '15 at 19:31
  • Thanks. However I don't need SIM contacts and method of getting contacts is the same as mine. – Illia Vlasov Aug 01 '15 at 17:22

1 Answers1

0

Use this,

  private void displayContacts() {

    ContentResolver cr = getContentResolver();
    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
            null, null, null, null);
    if (cur.getCount() > 0) {
        while (cur.moveToNext()) {
            String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
            String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            if (Integer.parseInt(cur.getString(
                    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));
                    Toast.makeText(NativeContentProvider.this, "Name: " + name + ", Phone No: " + phoneNo, Toast.LENGTH_SHORT).show();
                }
                pCur.close();
            }
        }
    }
}
Dchaser88
  • 124
  • 10
  • taken from https://github.com/KartikAgarwal/ContactsBook/blob/master/NewContentProviderSample/src/com/ts/contentprovider/NativeContentProvider.java – Dchaser88 Aug 01 '15 at 18:02