Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You have three questions here:</p> <ol> <li>How to enumerate the contact groups for a given account.</li> <li>How to mark a group as special.</li> <li>How to take action on contacts in that group.</li> </ol> <p>So, going down the list...</p> <p><strong>1. Enumerating contact groups</strong></p> <p>The <a href="http://developer.android.com/reference/android/provider/Contacts.Groups.html" rel="nofollow" title="Contacts.Groups">ContactsContract.Groups</a> table stores the list of contact groups on the system. So, you'll want to issue a query that looks like this:</p> <pre><code>public Loader&lt;Cursor&gt; onCreateLoader(int i, Bundle bundle) { Uri uri = ContactsContract.Groups.CONTENT_URI; Log.i(TAG, "URI: " + uri); String[] projection = new String[] { ContactsContract.Groups._ID, ContactsContract.Groups.TITLE }; return new CursorLoader(this, uri, projection, null, null, null); } </code></pre> <p>This loader will get you the list of all the groups on the system, and their database IDs.</p> <h3>How to mark a group as special</h3> <p>This is something your application will need to take care of. Just maintain a list of group IDs that are in your special list.</p> <p>To determine whether a contact is in the "special" group, you can query the <a href="http://developer.android.com/reference/android/provider/ContactsContract.Data.html" rel="nofollow" title="ContactsContract.Data">ContactsContract.Data</a> table using a SQL <code>where</code> clause like the following:</p> <pre><code>String where = ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID + "=" + groupid + " AND " + ContactsContract.CommonDataKinds.GroupMembership.MIMETYPE + "='" + ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE + "'"; </code></pre> <p>where <code>groupid</code> is the database ID of the group you've marked as special. (If you have more than one group, start adding <code>OR</code> clauses.)</p> <p>(You need to check for the CONTENT_ITEM_TYPE mimetype because the <a href="http://developer.android.com/reference/android/provider/ContactsContract.Data.html" rel="nofollow" title="ContactsContract.Data">ContactsContract.Data</a> table is used for storing arbitrary metadata for contacts, and the meaning of the columns in that table vary by mimetype. <code>GroupMembership.GROUP_ROW_ID</code> is simply a pointer to the column <code>data1</code>.)</p> <p><strong>3. How to take action on contacts in that group</strong></p> <p>This depends on what you're trying to accomplish, but in general, you'll create a broadcast receiver that listens for <code>android.intent.action.PHONE_STATE</code>. If the state in <code>TelephonyManager.EXTRA_STATE</code> is listed as <code>TelephonyManager.EXTRA_STATE_RINGING</code>, then you can get the phone number by looking in <code>TelephonyManager.EXTRA_INCOMING_NUMBER</code> to find the phone number.</p> <p>(There's sample code for this attached to this article: <a href="http://www.krvarma.com/2010/08/detecting-incoming-and-outgoing-calls-in-android/" rel="nofollow">http://www.krvarma.com/2010/08/detecting-incoming-and-outgoing-calls-in-android/</a>)</p> <p>You'll then need to check the Contacts database again to find any known contacts with that phone number. You can do this using the <a href="http://developer.android.com/reference/android/provider/ContactsContract.PhoneLookup.html" rel="nofollow" title="ContactsContract.PhoneLookup">ContactsContract.PhoneLookup</a> table.</p> <pre><code>Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber)); resolver.query(uri, new String[]{PhoneLookup.DISPLAY_NAME,... </code></pre> <p>That will get you the Contact's database ID in the <code>ContactsContract.Contacts</code> table. The last step here is to match that up with the <code>ContactsContract.RawContacts</code> rows for that contact (search using the <code>CONTACT_ID</code> column), and find the set of RawContacts _IDs that represent that contact. You'll then use this to search through the group membership table, as described above.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload