Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a number in my contacts is registered to Whatsapp?

But I need this with numbers with country codes (like starting with +123 456 78 90 -that's the way I add the number as a contact) I need a function/class to check if this number has Whatsapp. I need this for an .apk I made in Android Studio.

like image 713
erdem589 Avatar asked Nov 25 '25 05:11

erdem589


1 Answers

 private void getAllWhatsappNumbers()
{
    //This class provides applications access to the content model.
    ContentResolver cr = context.getContentResolver();

//RowContacts for filter Account Types
    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 {
                    //whatsappContactId for get Number,Name,Id ect... from  ContactsContract.CommonDataKinds.Phone
                    String whatsappContactId = contactCursor.getString(contactCursor.getColumnIndex(ContactsContract.RawContacts.CONTACT_ID));

                    if (whatsappContactId != null) {
                        //Get Data from ContactsContract.CommonDataKinds.Phone of Specific CONTACT_ID
                        Cursor whatsAppContactCursor = 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.CONTACT_ID + " = ?",
                                new String[]{whatsappContactId}, null);

                        if (whatsAppContactCursor != null) {
                            whatsAppContactCursor.moveToFirst();
                            String id = whatsAppContactCursor.getString(whatsAppContactCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));
                            String name = whatsAppContactCursor.getString(whatsAppContactCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                            String number = whatsAppContactCursor.getString(whatsAppContactCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                            Log.d("log1","no:"+number+"name:"+name);
                            whatsAppContactCursor.close();

                            //Add Number to ArrayList
                            myWhatsappContactsNumbers.add(number);
                        }
                    }
                } while (contactCursor.moveToNext());
                contactCursor.close();
            }
        }
    }
}

With this function provided, we can get contact list on "com.whatsapp" We put this contact list in to the array list so we need a function to show us we the number we sent is in the list (true) or not (false). How can we achieve that?

like image 57
erdem589 Avatar answered Nov 27 '25 21:11

erdem589



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!