List WhatsApp contacts

1

Is there any way to get the contacts that have Whatsapp between all the contacts obtained from the phone?

My tests have only been to obtain all the contacts and obtain their fields, to see if there were any "whatsapp ...":

ContentResolver cr = getContentResolver();
    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
            null, null, null, null);

and then I go through all the rows getting their fields

String[] columnNames = cur.getColumnNames();

for (String columName: columnNames){
    int index = cur.getColumnIndex(columName);
    String value = cur.getString(index);
    Log.d(TAG, "columName: " + columName +" =" + value);
}

and of all the names of fields, I can not differentiate any data to determine if I have it in whatsapp

I do not dominate the subject of content containers too much, maybe there is an exclusive of whatasapp if it is installed ...

Any suggestions on how to do it?

    
asked by Webserveis 07.09.2017 в 17:17
source

1 answer

2

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"/>
    
answered by 07.09.2017 / 18:14
source