how to get whats app contacts' phone numbers

2.2k views Asked by At

Hi I am currently trying to implement WhatsApp with an application I am working on. I want to be able to display all the WhatsApp contacts that a user has along with their phone number. So far I have been able to grab the name of their WhatsApp contact but I have not been able to grab the phone number of their WhatsApp contact, here is the code that I am using to get the list of a users WhatsApp contacts.

cursor = cr.query(ContactsContract.RawContacts.CONTENT_URI, new String[] {
    ContactsContract.RawContacts.CONTACT_ID, ContactsContract.RawContacts.DISPLAY_NAME_PRIMARY
}, ContactsContract.RawContacts.ACCOUNT_TYPE + "= ?", new String[] {
    "com.whatsapp"
}, null);
int contactNameColumn = cursor.getColumnIndex(ContactsContract.RawContacts.CONTACT_ID);
while (cursor.moveToNext()) {
    String name = cursor.getString(contactNameColumn);
    System.out.println(name);
    Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] {
        Integer.toString(contactNameColumn)
    }, null);
    while (pCur.moveToNext()) {
        String phonenumber = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
        System.out.println("PHONE: " + phonenumber);
    }
    pCur.close();
}
cursor.close();

When I print out the name it prints it out correctly, I then use that id to query the data table in order to get their phone number, but that always returns null, can anyone help me out please.

Thanks

2

There are 2 answers

0
Chandrakanth On

Change your pCur like this

     if (name != null) {
                    Cursor pCur = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]{name}, null);
                    //Iterate cursor here...
                    }

And it is better to use proper naming conventions for variables. Why because here the varible name represents the id of the conatact.

1
NehaK On

Use following code to get WhatsApp contacts from Phonebook with associated whatsApp number :

private void displayWhatsAppContacts() {

    final String[] projection = {
            ContactsContract.Data.CONTACT_ID,
            ContactsContract.Data.DISPLAY_NAME,
            ContactsContract.Data.MIMETYPE,
            "account_type",
            ContactsContract.Data.DATA3,
    };

    final String selection = ContactsContract.Data.MIMETYPE + " =? and account_type=?";
    final String[] selectionArgs = {
            "vnd.android.cursor.item/vnd.com.whatsapp.profile",
            "com.whatsapp"
    };

    ContentResolver cr = getContentResolver();
    Cursor c = cr.query(
            ContactsContract.Data.CONTENT_URI,
            projection,
            selection,
            selectionArgs,
            null);

    while (c.moveToNext()) {
        String id = c.getString(c.getColumnIndex(ContactsContract.Data.CONTACT_ID));
        String number = c.getString(c.getColumnIndex(ContactsContract.Data.DATA3));
        String name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

        Log.v("WhatsApp", "name " +name + " - number - "+number);

    }
    Log.v("WhatsApp", "Total WhatsApp Contacts: " + c.getCount());
    c.close();
}