如何让它们在WhatsApp的或其他应用程序所使用的Android通讯录

问题描述 投票:7回答:3

嗨,我想这是由其他应用(例如WhatsApp和Viber的)用于接触请看以下图片

请帮我这个问题谢谢

android contacts whatsapp viber
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));
}

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

    }

0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.