查询其他应用程序跳过同步联系人的联系人详细信息

时间:2013-04-29 10:14:53

标签: android android-contacts contactscontract

我正在进行查询以通过ContactsContract检索联系人详细信息(特别是他们的邮寄地址)。关键是我想跳过与WhatsApp,LinkedIn,Skype等其他应用程序同步的联系人......

我的代码是:

Uri uri = StructuredPostal.CONTENT_URI;
String[] sqlSelect = new String[] { StructuredPostal.FORMATTED_ADDRESS, StructuredPostal.DISPLAY_NAME, StructuredPostal.STREET, StructuredPostal.CITY, StructuredPostal.POSTCODE, StructuredPostal.COUNTRY };
String sqlWhere = StructuredPostal.MIMETYPE + "=?" + " AND " +
            "(" + StructuredPostal.STREET + " LIKE '%" + queryString + "%'" + " OR " + 
            StructuredPostal.CITY + " LIKE '%" + queryString + "%'" + " OR " +
            StructuredPostal.POSTCODE + " LIKE '%" + queryString + "%'" + " OR " +
            StructuredPostal.COUNTRY + " LIKE '%" + queryString + "%')";
String[] sqlWhereParams = new String[]{ StructuredPostal.CONTENT_ITEM_TYPE };
String sortOrder = StructuredPostal.STREET + " ASC";

Cursor cursor = this.getContentResolver().query(uri, sqlSelect, sqlWhere,  sqlWhereParams, sortOrder);
    if (cursor.moveToFirst()) {
        do {
            String formattedAddress = cursor.getString(cursor.getColumnIndex(StructuredPostal.FORMATTED_ADDRESS));
            String displayName = cursor.getString(cursor.getColumnIndex(StructuredPostal.DISPLAY_NAME));
            String street = cursor.getString(cursor.getColumnIndex(StructuredPostal.STREET));
            String city = cursor.getString(cursor.getColumnIndex(StructuredPostal.CITY));
            String postcode = cursor.getString(cursor.getColumnIndex(StructuredPostal.POSTCODE));
            String country = cursor.getString(cursor.getColumnIndex(StructuredPostal.COUNTRY));
            Log.d(TAG, formattedAddress + " " + displayName + " " + street + " " + city + " " + postcode + " " + country);

        } while (cursor.moveToNext());
    }

LogCat输出正确打印我在地址簿中手动添加的联系人:

<postal_address> <contact_full_name> <street> <city> <postcode> <country>

但从第三方应用程序导入的那些应用程序正在打印(LinkedIn):

<email> <contact_full_name> <email> null null null

应该在哪里(如果邮政地址不可用):

null <contact_full_name> null null null null

第三方应用是否错误地使用了联系人?有没有办法跳过第三方同步联系人?

1 个答案:

答案 0 :(得分:0)

可以对已关联的帐户进行过滤,我决定只过滤来自Google(ContactsContract.RawContacts.ACCOUNT_TYPE + "= 'com.google')的帐户:

String sqlWhere = StructuredPostal.MIMETYPE + "=?" + " AND " + ContactsContract.RawContacts.ACCOUNT_TYPE + "= 'com.google' AND " +
            "(" + StructuredPostal.STREET + " LIKE '%" + queryString + "%'" + " OR " + 
            StructuredPostal.CITY + " LIKE '%" + queryString + "%'" + " OR " +
            StructuredPostal.POSTCODE + " LIKE '%" + queryString + "%'" + " OR " +
            StructuredPostal.COUNTRY + " LIKE '%" + queryString + "%')";