如何获取在android中的whatsapp或其他应用程序中使用的联系人

时间:2022-05-25 00:20:59

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

您好我想获得其他应用程序使用的联系人(如whatsapp或viber)请参见下图

如何获取在android中的whatsapp或其他应用程序中使用的联系人

please help me about this issue thanks

请帮我解决这个问题谢谢

2 个解决方案

#1


24  

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

使用清单中的READ_CONTACTS权限,您可以根据帐户类型获得同步的联系人。对于WhatsApp来说,它是“com.whatsapp”。所以:

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

#2


0  

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

myWhatsappContacts ArrayList将包含whatsapp应用程序中存在的所有电话号码。

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

    }

#1


24  

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

使用清单中的READ_CONTACTS权限,您可以根据帐户类型获得同步的联系人。对于WhatsApp来说,它是“com.whatsapp”。所以:

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

#2


0  

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

myWhatsappContacts ArrayList将包含whatsapp应用程序中存在的所有电话号码。

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

    }