C3pO
C3pO

Reputation: 81

Get contact per account type

Can somebody help me on how to get all contacts per account? Meaning, I want to put a condition which will determine if the contact is from the phone (created by user) or from google and some other sync sources because as of now I was getting all contacts and its the combination of all sync sources e.g. local contacts, google or even yahoo contacts ?

Upvotes: 0

Views: 606

Answers (1)

Sergii Pechenizkyi
Sergii Pechenizkyi

Reputation: 22232

Can somebody help me on how to get all contacts per account?

You can use next snippet to retrieve contacts for a particular account type:

String where = RawContacts.ACCOUNT_TYPE+ "=?";
String[] args = { accountType };
Cursor contacts = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, where, args, null);

int numberIndex = contacts.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
int displayNameIndex = contacts.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
for (contacts.moveToFirst(); !contacts.isAfterLast(); contacts.moveToNext()) {
    String number = contacts.getString(numberIndex);
    String displayName = contacts.getString(displayNameIndex);
    // do something with account contacts
}
contacts.close();

To filter plain phone contacts (not connected to any account) you can use:

String where = RawContacts.ACCOUNT_TYPE+ " IS NULL";

Upvotes: 1

Related Questions