Search code examples
androidfacebooktwitterlinkedin-apiandroid-contacts

how to get all social accounts linked with the particular contact in android from phonebook?


I fetched all device contacts from phonebook. Now i want to fetch linked accounts(facebook,twitter,instagram,LinkedIn)urls from that particular contact that is fetched from phonebook.What should i do?

Here is the code to fetch the contacts.

public Cursor getContactsCursor(FragmentActivity activity) {
        Cursor cursor = null;
        try {
            String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + "= 0" + " OR " + ContactsContract.Contacts.IN_VISIBLE_GROUP + "= 1";
            String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
            ContentResolver cr = activity.getContentResolver();
            return cr.query(ContactsContract.Contacts.CONTENT_URI, null, selection, null, sortOrder);
        } catch (Exception e) {
            AppLogger.e(Helper.class.getSimpleName(), e.getMessage());
            return cursor;
        }
    }

Now i don't know how to fetch the accounts (like facebook, linkedin etc) linked with the particular contact.

Can someone please guide me.

Update : In below attached image, On clicking the section highlighted in red, opens the linked in user profile in browser. Hence i am willing to fetch the field which is used to open the user profile page.

enter image description here

Thanks in advance.


Solution

  • You'll need to figure out the exact MIMETYPE of all accounts you're interested in, for example, Google+'s MIMETYPE is: vnd.android.cursor.item/vnd.googleplus.profile

    You can dump all MIMETYPEs for a contact and figure out manually which you need:

    // make sure you import Data from: ContactsContract.Data
    String[] projection = new String[] { Data.MIMETYPE };
    String selection = Data.CONTACT_ID + " = '"+ contactId + "'";
    Cursor cursor = getContentResolver().query(Data.CONTENT_URI, projection, selection, null, null);
    DatabaseUtils.dumpCursor(cursor);
    cursor.close();
    

    Once you have a fixed list of the MIMETYPEs you want, you can query the info in them for a specific contact:

    // Add more
    String[] mimetypes = new String[] { 
        "vnd.android.cursor.item/vnd.googleplus.profile",
        "vnd.android.cursor.item/vnd.com.whatsapp.profile" 
    };
    
    // Usually the interesting info is on the first few fields, modify this if needed
    String[] projection = new String[] { Data.DATA1, Data.DATA2, Data.DATA3, Data.DATA4 };
    String selection = Data.CONTACT_ID + " = '"+ contactId + "' AND " + Data.MIMETYPE + " IN (?,?)";
    
    Cursor cursor = getContentResolver().query(Data.CONTENT_URI, projection, selection, mimetypes, null);
    DatabaseUtils.dumpCursor(cursor);
    cursor.close();
    

    UPDATE:

    In case of linkedin, the mimetype is indeed: vnd.android.cursor.item/vnd.com.linkedin.android.profile. Regarding your comment about not having the profile url, in Data1 you should have some long ID like AC...UQ4 (about 40 characters).

    Then your url is: https://www.linkedin.com/profile/view?id=<data1Id> like: https://www.linkedin.com/profile/view?id=AC...UQ4