首页 > 解决方案 > 是否可以从通讯录中过滤掉 facebook、whatsapp 联系人?

问题描述

我想从android中的电话簿中过滤同步的facebook联系人,是否有任何投影内容://方案,以便检索内容。

这是我当前的查询给定的 URI,返回结果集上的光标。

public static Cursor getContactList(ContentResolver cr) {
        // reading all data in descending order according to DATE
        Cursor curContact =  cr.query(ContactsContract.Contacts.CONTENT_URI,
                null, null, null, null);
        return curContact;
    }

标签: javaandroidfacebook-graph-apiandroid-intentandroid-contacts

解决方案


是的...您可以在给定帐户类型的情况下获得同步的联系人。对于 WhatsApp,它是“com.whatsapp”(*尚未测试 facebook 的帐户类型)。所以只需定义帐户类型,如下例所示

Cursor c = getContentResolver().query(
        RawContacts.CONTENT_URI,
        new String[] { RawContacts.CONTACT_ID, RawContacts.DISPLAY_NAME_PRIMARY },
        RawContacts.ACCOUNT_TYPE + "= ?",
        new String[] { "com.whatsapp","facebook" },
        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));
}

推荐阅读