This is a way to get them, through a ContactsContract for "Whatsapp" type accounts , you can get the data in a cursor:
Cursor contactCursor = cr.query(ContactsContract.RawContacts.CONTENT_URI, new String[]{ContactsContract.RawContacts._ID, ContactsContract.RawContacts.CONTACT_ID}, ContactsContract.RawContacts.ACCOUNT_TYPE + "= ?", new String[]{"com.whatsapp"}, null);
As an example this method which can return in an ArrayList data like ID, Name, Contact number and status:
private static final int CONTACT_ID = 0, DISPLAY_NAME = 1 , NUMBER = 2 , STATUS = 3;
method:
private ArrayList<String> getContactNumbers(int datatype){
ArrayList<String> contactData = new ArrayList<>();
ContentResolver cr = getContentResolver();
Cursor contactCursor = cr.query(ContactsContract.RawContacts.CONTENT_URI, new String[]{ContactsContract.RawContacts._ID, ContactsContract.RawContacts.CONTACT_ID}, ContactsContract.RawContacts.ACCOUNT_TYPE + "= ?", new String[]{"com.whatsapp"}, null);
if (contactCursor != null) {
if (contactCursor.getCount() > 0) {
if (contactCursor.moveToFirst()) {
do {
String contactId = contactCursor.getString(contactCursor.getColumnIndex(ContactsContract.RawContacts.CONTACT_ID));
if (contactId != null) {
Cursor c = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
new String[]{ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.STATUS}, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]{contactId}, null);
if (c != null) {
c.moveToFirst();
String data = "";
switch (datatype){
case CONTACT_ID:
data = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));
break;
case DISPLAY_NAME:
data = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
break;
case NUMBER:
data = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
break;
case STATUS:
data = String.valueOf(c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.STATUS)));
break;
default:
break;
}
c.close();
contactData.add(data);
}
}
} while (contactCursor.moveToNext());
contactCursor.close();
}
}
}
return contactData;
}
For example to obtain all the numbers of the contacts would be done in this way:
ArrayList<String> contactos = getContactNumbers(NUMBER);
You can modify this method so that instead of obtaining an ArrayList of String you can obtain an ArrayList of Contact objects that has as properties ID, Name, Contact number, status or the data you want
Remember it is important to have the permissions defined:
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.WRITE_CONTACTS"/>