如何从Android获取whatsapp联系人?

时间:2016-02-17 04:57:36

标签: android android-contentprovider whatsapp android-contentresolver

我已经尝试通过手机获得whatsapp联系,我得到了whatsapp联系人的总数,但是来自=IFERROR(INDEX($B2:$B100,MATCH(1,($A2:$A100=$H1)*($E2:$E100=I$1),0),3),"NONO") 如何获得whatsapp我不知道的号码和名称。我试图找到解决方案,但无法得到确切的解决方案。请帮我。

我将代码放在下面。

RawContacts

1 个答案:

答案 0 :(得分:53)

我找到了问题的解决方案,所以我把答案放在这里..可能对其他人有用..

首先阅读并尝试了解Android Contacts Data Store的图表,之后很容易理解整个联系流程。在片段下面给出了三层数据模型图。

代码段

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

这里是演示Android联系人数据存储图 enter image description here