Android contact request

I have a problem with requesting phone book contacts. What I need to do is get a list of contacts that have both phone and email, entered or of a certain type.

Mostly like this:

public static final String SELECTION =
    "("+ContactsContract.Contacts.HAS_PHONE_NUMBER +"='1') OR " + RawContacts.ACCOUNT_TYPE + "='" + Constants.ACCOUNT_TYPE + "'";

Now the problem is that RawContacts.ACCOUNT_TYPEdoes not exist in ContactsContract.Contacts.CONTENT_URIwhich I use with my request. I assume that I will need to join another table, but I have no idea how to do this.

Can someone help me here please?

+3
source share
2 answers

ContactsContract.RawContacts.Entity. , Entity . , - .

Uri rawContactUri = ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactId);
 Uri entityUri = Uri.withAppendedPath(rawContactUri, Entity.CONTENT_DIRECTORY);
 Cursor c = getContentResolver().query(entityUri,
          new String[]{RawContacts.SOURCE_ID, Entity.DATA_ID, Entity.MIMETYPE, Entity.DATA1},
          null, null, null);
 try {
     while (c.moveToNext()) {
         String sourceId = c.getString(0);
         if (!c.isNull(1)) {
             String mimeType = c.getString(2);
             String data = c.getString(3);
             //decide here based on mimeType, see comment later
         }
     }
 } finally {
     c.close();
 }

mimeType

, mimeType Phone.CONTENT_ITEM_TYPE, DATA1 , Email.CONTENT_ITEM_TYPE, DATA1 .

HAS_PHONE_NUMBER, .

+2

, Email.CONTENT_URI, "vnd.android.cursor.item/email_v2" MIME, ..

+1

Source: https://habr.com/ru/post/1751351/


All Articles