3

Is it possible to get the connections of any contact from contact list of device? Connection like email id of google+ account, and other third party api,I am searching the query of it. But did not get the proper solution. So please friends help me to sort out from these problem. Thanks in advance

Ravindra Kushwaha
  • 7,846
  • 14
  • 53
  • 103

3 Answers3

3

In the Contacts provider, the "connections" of a Contact are just all the entries in RawContacts for that particular CONTACT_ID. And their data is stored in the Data table.

Therefore, to get all the info on all the connections of a contact, it's enough to query the Data table with the appropriate filter. For example:

private static void getConnections(Context context, int contactId)
{
    // Read all data for contactId
    String selection = RawContacts.CONTACT_ID + " = ?";
    String[] selectionArgs = new String[] { String.valueOf(contactId) };

    Cursor c = context.getContentResolver().query(ContactsContract.Data.CONTENT_URI, null, selection, selectionArgs, null);
    while (c.moveToNext())
    {
        String accountType = c.getString(c.getColumnIndexOrThrow(ContactsContract.RawContacts.ACCOUNT_TYPE));
        String accountName = c.getString(c.getColumnIndexOrThrow(ContactsContract.RawContacts.ACCOUNT_NAME));
        String dataMimeType = c.getString(c.getColumnIndexOrThrow(ContactsContract.Data.MIMETYPE));
        String dataValue = c.getString(c.getColumnIndexOrThrow(ContactsContract.Data.DATA1));

        Log.d("contactInfo", accountName + " (" + accountType + ") - " + dataMimeType + " - " + dataValue);
    }

    c.close();
}

You can filter this data by using more restrictive selection / selectionArgs arguments (such as for a specific ACCOUNT_TYPE, MIMETYPE, and so forth).

Of course, you need the appropriate permission in AndroidManifest.xml:

<uses-permission android:name="android.permission.READ_CONTACTS" />
matiash
  • 54,791
  • 16
  • 125
  • 154
  • Thanks a lot bro..i will soon try yours code..And if it works fine for me the of course i will mark yours answer...Again thanks :) – Ravindra Kushwaha Dec 25 '14 at 04:51
  • hello brother... I have used yours code but the problem is that i am getting all contact details instead of passing the contactid to fetch particular details of user from contact list...whats the problem is that i am not getting it properly... – Ravindra Kushwaha Dec 25 '14 at 09:53
  • @RavindraKushwaha sorry, but what do you mean by "not properly"? – matiash Dec 25 '14 at 13:37
  • Sorry brother..i mean to say that i am getting the problem that when i used yours code than it show me all user(Contact list ) account name and account type – Ravindra Kushwaha Dec 25 '14 at 14:05
  • @RavindraKushwaha Actually, it should show all the fields (`dataMimeType` and `dataValue`) for all connections of a particular contact. – matiash Dec 25 '14 at 21:04
  • hello brother it does not show whatapp and facebook connection from device?? Do you have an any idea about it..thanks bro – Ravindra Kushwaha Dec 26 '14 at 12:50
  • 1
    @RavindraKushwaha Strange, I've tested it and it works. Are you sure you're passing in a contact id that _does_ have a Whatsapp connection? To get the list of contacts that do have one, see http://stackoverflow.com/a/24276984/82788 – matiash Dec 26 '14 at 13:25
  • hello brother i do not want to bother you...and please do not worry about the bounty..i am assuring you that i will provide only you these bounty with in 2- 3 days... these is the code which i am using to get the third api connection of contact list contacts.. please check it Cursor c = cr.query(ContactsContract.RawContacts.CONTENT_URI, new String[]{ContactsContract.RawContacts.ACCOUNT_NAME, ContactsContract.RawContacts.ACCOUNT_TYPE}, ContactsContract.RawContacts.CONTACT_ID +"=?", new String[]{String.valueOf(contactId)}, null); – Ravindra Kushwaha Dec 29 '14 at 04:54
  • @RavindraKushwaha Yes, that code will give you the raw contacts associated to a contact, but not their data. Sorry but now I am somewhat confused as to what you're trying to achieve... :( – matiash Dec 29 '14 at 13:37
  • Sir i want to retrieve from contact list user which can have(facebook,whatapp,google+) connection..Please can you give one proper query for it by that i can resolve my problem....thanks bro – Ravindra Kushwaha Dec 29 '14 at 13:48
1
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));
            Cursor cur1 = cr.query( 
                    ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
                    ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", 
                            new String[]{id}, null); 
            while (cur1.moveToNext()) { 
                //to get the contact names
                String name=cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                Log.e("Name :", name);
                String email = cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
                Log.e("Email", email);
                if(email!=null){
                    names.add(name);
                }
            } 
            cur1.close();
        }
    }
Akash
  • 681
  • 3
  • 17
0

This is the solution, it's using a Whatsapp-like third party API:

Cursor connectionCur = cr.query(ContactsContract.RawContacts.CONTENT_URI,
                            new String[]{ContactsContract.RawContacts.ACCOUNT_NAME, ContactsContract.RawContacts.ACCOUNT_TYPE},
                            ContactsContract.RawContacts.CONTACT_ID +"=?",
                            new String[]{String.valueOf(id)},
                            null);
System.out.println("TOTAL NUMBERS OF COUNTS====================="+connectionCur.getCount());
if (connectionCur != null && connectionCur.getCount() >0)
{
    while (connectionCur.moveToNext()) 
    {
        String accountName = connectionCur.getString(connectionCur.getColumnIndex(ContactsContract.RawContacts.ACCOUNT_NAME));
        String accountType = connectionCur.getString(connectionCur.getColumnIndex(ContactsContract.RawContacts.ACCOUNT_TYPE));

        if(accountType.contains("whatsapp"))
        {
            Toast.makeText(LoginScreen.this, "whatsapp == "+accountType, Toast.LENGTH_SHORT).show();
        }
        else if(accountType.contains("google"))
        {
            Toast.makeText(LoginScreen.this, "google == "+accountType, Toast.LENGTH_SHORT).show();
        }
        else if(accountType.contains("facebook"))
        {
            Toast.makeText(LoginScreen.this, "facebook == "+accountType, Toast.LENGTH_SHORT).show();
        }
        else if(accountType.contains("twitter"))
        {
            Toast.makeText(LoginScreen.this, "twitter == "+accountType, Toast.LENGTH_SHORT).show();
        }
        else if(accountType.contains("linkedin"))
        {
            Toast.makeText(LoginScreen.this, "linkedin == "+accountType, Toast.LENGTH_SHORT).show();
        }
        System.out.println("a========="+accountName+"   b====="+accountType);   
    }
}
AStopher
  • 4,207
  • 11
  • 50
  • 75
Ravindra Kushwaha
  • 7,846
  • 14
  • 53
  • 103