如何在Android设备上获取WhatsApp或其他应用程序中使用的联系人

7

你好,我想获取其他应用程序(如WhatsApp或Viber)使用的联系方式,请参见下面的图片。

enter image description here

请帮助我解决这个问题,谢谢。


从这里开始:http://developer.android.com/samples/BasicContactables/index.html - ditkin
你好,如何查找类似于1234567890的前任手机号码,并确定它是否在WhatsApp中使用,以及如何在我的应用程序联系人列表中查找其使用情况? - Vijay Rajput
你尝试了什么?你正在尝试让代码工作时遇到了问题吗? - ditkin
到目前为止还没有尝试任何代码,只是在搜索。如果您有任何解决方案,请告诉我如何做? - Vijay Rajput
1
请使用以下链接http://stackoverflow.com/a/35453979/4395114查看整个描述和Android联系人数据存储表的流程。 - Mansukh Ahir
3个回答

24

在您的清单文件中添加 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));
}

@matiash 返回的是联系人姓名,那电话号码呢? - CodeGuru
@CodeGuru 您可以使用此查询返回的联系人 ID 查询数据表。联系人的所有信息都应该在那里。 - matiash
@VijayRajput,我怎样才能获取WhatsApp联系人的电话号码?我只能获取到名字。 - Mansukh Ahir

0

System.out.print("姓名:"+cursor.getString(contactNameColumn) +"\n "+" 电话:" + cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));


0

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

    }

谢谢,这帮了我很多。 (是否可以通过将数字发送到WhatsApp并获取响应来检查该数字是否在WhatsApp上?https://stackoverflow.com/questions/61269646/get-check-whatsapp-numbers-from-android-programmatically) - user8462556

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接