PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, November 6, 2022

[FIXED] How to get whatsapp Contacts from Android Programmatically?

 November 06, 2022     android, android-contacts, android-contentresolver, contacts, whatsapp     No comments   

Issue

I have to try to get WhatsApp contacts from phone and I get a total Count of WhatsApp contact but from RawContacts how to get WhatsApp numbers and names that I don't know. I have tried to find a solution but can't get the exact solution for that. Please help me.

I put my code below.

ContentResolver cr = context.getContentResolver();

Cursor c = 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);

                ArrayList<String> myWhatsappContacts = new ArrayList<>();

                String projection[] = { ContactsContract.CommonDataKinds.Phone.NUMBER };

                if(c != null) {
                    if (c.getCount() > 0) {
                        while (c.moveToNext()) {

                            String whatsappContactId = c.getString(c.getColumnIndex(ContactsContract.RawContacts.Data._ID));

                            Cursor dataCursor = cr.query(
                                    ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                                    projection,
                                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
                                    new String[]{whatsappContactId}, null);
                            // You can also read RawContacts.CONTACT_ID to read the
                            // ContactsContract.Contacts table or any of the other related ones.
                            String number = dataCursor.getString(dataCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NORMALIZED_NUMBER));
                            myWhatsappContacts.add(number);

                        }
                    }
                }

                showLogI(TAG, " WhatsApp contact size :  " + myWhatsappContacts.size());

Solution

I have found Solution of my question so i put my answer here.. may it useful for others..

First Read and try to understand diagram of Android Contacts Data Store after that it's easy to understand whole contact flow. Three tier data model diagram is given below the snippets.

Code Snippets

//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);

//ArrayList for Store Whatsapp Contact
ArrayList<String> myWhatsappContacts = new ArrayList<>();

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));

                        whatsAppContactCursor.close();

                        //Add Number to ArrayList
                        myWhatsappContacts.add(number);

                        showLogI(TAG, " WhatsApp contact id  :  " + id);
                        showLogI(TAG, " WhatsApp contact name :  " + name);
                        showLogI(TAG, " WhatsApp contact number :  " + number);
                    }
                }
            } while (contactCursor.moveToNext());
            contactCursor.close();
        }
    }
}

showLogI(TAG, " WhatsApp contact size :  " + myWhatsappContacts.size());

here is the show Android Contact Data Store Diagram enter image description here



Answered By - Mansukh Ahir
Answer Checked By - Cary Denson (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing