7

嗨,我想获得其他应用程序(如whatsapp或viber)使用的联系方式,请参见下图

在此处输入图像描述

请帮我解决这个问题谢谢

4

3 回答 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));
}
于 2014-06-18T04:17:06.817 回答
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))); 

    }
于 2018-03-16T11:04:06.930 回答
0

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

于 2019-02-08T05:32:33.147 回答