How to get contacts which are used in whatsapp or other application in android

前端 未结 3 1219
日久生厌
日久生厌 2020-12-08 06:00

Hi i want to get contact which are used by other application (like whatsapp or viber ) please see in below image

相关标签:
3条回答
  • 2020-12-08 06:22

    myWhatsappContacts ArrayList will contain all the phone numbers that are present in your whatsapp Application.

    Cursor cursor = getContentResolver().query(ContactsContract.Data.CONTENT_URI 
                      ,new String[] {ContactsContract.Data._ID
                                   ,ContactsContract.Data.DISPLAY_NAME
                                   ,ContactsContract.CommonDataKinds.Phone.NUMBER 
                                   ,ContactsContract.CommonDataKinds.Phone.TYPE}
                      ,ContactsContract.Data.MIMETYPE + "='" + ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE
                      + "' AND " + ContactsContract.RawContacts.ACCOUNT_TYPE + "= ?"
                      ,new String[] { "com.whatsapp" }
                      , null);
    
        while (cursor.moveToNext())
        {
    
            myWhatsappContacts.add(cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER))); 
    
        }
    
    0 讨论(0)
  • 2020-12-08 06:24

    System.out.print("Name : "+cursor.getString(contactNameColumn) +"\n "+" Phone Number" + cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));

    0 讨论(0)
  • 2020-12-08 06:26

    With the READ_CONTACTS permission in your manifest, you can get synced contacts given the account type. For WhatsApp it's "com.whatsapp". So:

    Cursor c = getContentResolver().query(
            RawContacts.CONTENT_URI,
            new String[] { RawContacts.CONTACT_ID, RawContacts.DISPLAY_NAME_PRIMARY },
            RawContacts.ACCOUNT_TYPE + "= ?",
            new String[] { "com.whatsapp" },
            null);
    
    ArrayList<String> myWhatsappContacts = new ArrayList<String>();
    int contactNameColumn = c.getColumnIndex(RawContacts.DISPLAY_NAME_PRIMARY);
    while (c.moveToNext())
    {
        // You can also read RawContacts.CONTACT_ID to read the
        // ContactsContract.Contacts table or any of the other related ones.
        myWhatsappContacts.add(c.getString(contactNameColumn));
    }
    
    0 讨论(0)
提交回复
热议问题