13

It is possible to do it in one query?

As far as I know content uri of sim contacts is content://icc/adn

noisy
  • 6,495
  • 10
  • 50
  • 92
  • Please Checkout a library on [Github](https://github.com/nitiwari-dev/android-contact-extractor) to fetch contacts with simple easy to use api. – Nitesh Tiwari May 12 '17 at 09:56

1 Answers1

21

This is very easy! :)

Cursor cursor = mContentResolver.query(
   RawContacts.CONTENT_URI,
   new String[]{RawContacts._ID,RawContacts.ACCOUNT_TYPE},
   RawContacts.ACCOUNT_TYPE + " <> 'com.anddroid.contacts.sim' "
    + " AND " + RawContacts.ACCOUNT_TYPE + " <> 'com.google' " //if you don't want to google contacts also
   ,
   null,
   null);
noisy
  • 6,495
  • 10
  • 50
  • 92
  • 1
    Can you please write the code of how to get the number and name from this cursor. i hvae write the code as "while(cursor.moveToNext()){String phoneNumber = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); Log.i("all contacts",phoneNumber+"->"+name); } – Nency Aug 28 '12 at 11:24
  • but got the error like ": Bad request for field slot 0,-1. numRows = 46, numColumns = 2 – Nency Aug 28 '12 at 11:30
  • 1
    'com.anddroid.contacts.sim' is a manual value. There is any constant refer to it. – tana Apr 10 '14 at 07:21
  • @tana How do you know which possible constants are available for the current device? Is there a way to get the correct one for SIM/local storage of contacts, no matter the device? – android developer May 15 '16 at 11:52
  • sorry I dont know the constant – tana May 22 '16 at 10:26