user2192174
user2192174

Reputation: 79

How to get contacts associated with social media(google,yahoo,...) synced with phone, in android

My issue is, when I'm fetching contacts from phone, it will get all the contacts (from phone, SIM, social accounts etc)

i want to know, if I can get contacts from social accounts separately?

Upvotes: 7

Views: 832

Answers (1)

matiash
matiash

Reputation: 55340

As long as the contacts for the applications are synced, you can query the RawContacts (with the READ_CONTACTS permission in your manifest) and get them filtered by account type. For example:

Cursor c = getContentResolver().query(
    RawContacts.CONTENT_URI,
    new String[] { RawContacts.CONTACT_ID, RawContacts.DISPLAY_NAME_PRIMARY },
    RawContacts.ACCOUNT_TYPE + "= ?",
    new String[] { <account type here> },
    null);

ArrayList<String> myContacts = new ArrayList<String>();
int contactNameColumn = c.getColumnIndex(RawContacts.DISPLAY_NAME_PRIMARY);
while (c.moveToNext())
{
    // You can also read RawContacts.CONTACT_ID to read the
    // ContactsContract.Contacts table or any of the other related ones.
    myContacts.add(c.getString(contactNameColumn));
}

It's just a matter of supplying the appropriate account type. For example:

  • "com.google" for Gmail,
  • "com.whatsapp" for WhatsApp,
  • "com.skype.contacts.sync" for Skype,
  • &c.

Upvotes: 1

Related Questions