How to get contacts which are used in whatsapp or other application in android
Asked Answered
J

3

7

Hi i want to get contact which are used by other application (like whatsapp or viber ) please see in below image

enter image description here

please help me about this issue thanks

Jackknife answered 18/6, 2014 at 3:26 Comment(5)
Start here: developer.android.com/samples/BasicContactables/index.htmlUnvoice
hi how to find ex mobile number like 1234567890 and its used in whats app so how to find its used in whatsapp in my application contact list?Jackknife
What have tried? Are there problems with the code you are trying to get to work?Unvoice
still now not tring any code only search for that if you have any solution explain me how to do that?Jackknife
use this link https://mcmap.net/q/477834/-how-to-get-whatsapp-contacts-from-android-programmatically for whole description and with flow of data store table of android contactsTorrential
S
24

With the READ_CONTACTS permission in your manifest, you can get synced contacts given the account type. For WhatsApp it's "com.whatsapp". So:

Cursor c = getContentResolver().query(
        RawContacts.CONTENT_URI,
        new String[] { RawContacts.CONTACT_ID, RawContacts.DISPLAY_NAME_PRIMARY },
        RawContacts.ACCOUNT_TYPE + "= ?",
        new String[] { "com.whatsapp" },
        null);

ArrayList<String> myWhatsappContacts = 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.
    myWhatsappContacts.add(c.getString(contactNameColumn));
}
Spaceband answered 18/6, 2014 at 4:17 Comment(3)
@Spaceband it's returning Contact name, what about the numbers?Meatus
@Meatus You can query the Data table with the contact id returned by this query. All the information for the contact should be there.Spaceband
@VijayRajput how can i get whatsapp contact number? I am only getting name.Torrential
V
0

myWhatsappContacts ArrayList will contain all the phone numbers that are present in your whatsapp Application.

Cursor cursor = getContentResolver().query(ContactsContract.Data.CONTENT_URI 
                  ,new String[] {ContactsContract.Data._ID
                               ,ContactsContract.Data.DISPLAY_NAME
                               ,ContactsContract.CommonDataKinds.Phone.NUMBER 
                               ,ContactsContract.CommonDataKinds.Phone.TYPE}
                  ,ContactsContract.Data.MIMETYPE + "='" + ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE
                  + "' AND " + ContactsContract.RawContacts.ACCOUNT_TYPE + "= ?"
                  ,new String[] { "com.whatsapp" }
                  , null);

    while (cursor.moveToNext())
    {

        myWhatsappContacts.add(cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER))); 

    }
Venator answered 16/3, 2018 at 11:4 Comment(1)
thanks man. that helped a'lot. (Is it also possible to send a number to whatsApp and get response if that number is on whatsapp? #61270146 )Eijkman
R
0

System.out.print("Name : "+cursor.getString(contactNameColumn) +"\n "+" Phone Number" + cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));

Recidivate answered 8/2, 2019 at 5:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.