按帐户类型从Android设备过滤器获取联系人

时间:2015-06-15 03:14:36

标签: android android-contacts

我写了一个应用程序联系人同步。它将服务器之间的联系同步。当与用户haduy@lvsolution.vn完成同步时,我希望得到所有联系人的haduy用户。但是它可以在设备中获得所有联系,不会被haduy用户过滤掉。我只想联系haduy @ lvsolution帐户。

public void getContact()
{
    String phone = null;
    String name = null;
    listContact.clear();
    ContentResolver cr = getContentResolver();
    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
            null, null, null, null);

    if (cur.getCount() > 0) {
        while (cur.moveToNext()) {
            String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
            name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                System.out.println("name : " + name + ", ID : " + id);
                // get the phone number
                Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
                        new String[]{id}, null);
                while (pCur.moveToNext()) {
                   phone = pCur.getString(
                            pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    System.out.println("phone" + phone);
                }
                Contact contact = new Contact(name,phone);
                listContact.add(contact);
                pCur.close();
            }
        }
    }
}

1 个答案:

答案 0 :(得分:2)

您可以从account_type获取RawContatcs的联系人,如下所示。

String yourAccountType="";//ex: "com.whatsapp"
    Cursor c = getContentResolver().query(
            RawContacts.CONTENT_URI,
            new String[] { RawContacts.CONTACT_ID, RawContacts.DISPLAY_NAME_PRIMARY },
            RawContacts.ACCOUNT_TYPE + "= ?",
            new String[] { yourAccountType },
            null);

    ArrayList<String> contactList = 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.
        contactList.add(c.getString(contactNameColumn));
    }
    c.close();