質問

I am doing a query to retrieve contacts details (specifically their postal address) via ContactsContract. The point is that I would like to skip the contacts that are synchronized from other applications like WhatsApp, LinkedIn, Skype,...

My code is:

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

The LogCat output is printing correctly the contacts I manually added in my address book:

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

But the ones imported from third party apps are being printed like (LinkedIn):

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

Where it should be (if postal address is not available):

null <contact_full_name> null null null null

Are the third-party apps using incorrectly the contacts? Is there any way to skip the third-party synched contacts?

役に立ちましたか?

解決

It can be done filtering the account linked with, I decided to filter only the ones coming from 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 + "%')";
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top