I need to find out if a particular contact is a member of a given group. I 
have been working with the code below and have proved that it works using a 
handset running Gingerbread. However I have been trying to run it on my HTC 
One S running Android version 4.0.3 and it is failing. Has the API spec 
changed?

public boolean checkGroupMembership(String groupID, String contactID, Context 
ctx) {
            ContentResolver groupContentResolver = ctx.getContentResolver();
                        String select = "contact_id=" + contactID + 
                    " AND " + GroupMembership.GROUP_ROW_ID + " = " + groupID +
                    " AND mimetype='vnd.android.cursor.item/group_membership'";

            Cursor contactGroupCursor =  
                    groupContentResolver.query(
                        Data.CONTENT_URI, 
                        new String[] { GroupMembership.CONTACT_ID,  
GroupMembership.GROUP_ROW_ID, GroupMembership.IN_VISIBLE_GROUP }, 
                        select,
                        null, 
                        null);
            int records = contactGroupCursor.getCount();
            try {
            while(contactGroupCursor.moveToNext()){
                String ContactID = 
contactGroupCursor.getString(contactGroupCursor.getColumnIndexOrThrow(GroupMembership.CONTACT_ID));
                String groupRowId = 
contactGroupCursor.getString(contactGroupCursor.getColumnIndexOrThrow(GroupMembership.GROUP_ROW_ID));
                String InVisiblegroup = 
contactGroupCursor.getString(contactGroupCursor.getColumnIndexOrThrow(GroupMembership.IN_VISIBLE_GROUP));

                Log.d("DEBUG", "groupSourceId in checkGroupMembership: " + 
groupRowId + "  InVisiblegroup = " + InVisiblegroup);
                Log.d("DEBUG", "ContactID in checkGroupMembership: " + 
ContactID);          

                }       
            } 
            finally 
            {
                contactGroupCursor.close();
            }

            // see if the contact is in this group
            if (records > 0) {
                return true;
            }
            else {


            return false;
            }
        }

    }

In addition changing the query to purely find out what groups the contact 
is a member of:

String select = "contact_id=" + contactID;

results in the following debug output. In some cases groupRowID looks like 
a group ID, in others it is the contact number or the name of the contact. 
Note that I am using my home landline to call test and this is appearing. 
Also - I only have 10 groups defined on the handset and an id of 11, 12 and 
13 is printed in the output... It could be that I'm missing something? I 
definitely know that the contact_id I'm passing in is a member of the 
Coworkers group (group_ID 5 on my phone) but this is never returned in ICS. 
I have independently verified the group membership for HOME using contact 
manager apps on both handsets.

11-04 11:48:21.980: D/DEBUG(10145): select in checkGroupMembership : 
contact_id=133 11-04 11:48:21.990: D/DEBUG(10145): count of records in 
checkGroupMembership: 13 11-04 11:48:22.000: D/DEBUG(10145): groupRowId in 
checkGroupMembership: null InVisiblegroup = 1 11-04 11:48:22.000: 
D/DEBUG(10145): ContactID in checkGroupMembership: 133 11-04 11:48:22.000: 
D/DEBUG(10145): groupRowId in checkGroupMembership: InVisiblegroup = 1 
11-04 11:48:22.000: D/DEBUG(10145): ContactID in checkGroupMembership: 133 
11-04 11:48:22.000: D/DEBUG(10145): groupRowId in checkGroupMembership: 
004-414-83326995 InVisiblegroup = 1 11-04 11:48:22.000: D/DEBUG(10145): 
ContactID in checkGroupMembership: 133 11-04 11:48:22.000: D/DEBUG(10145): 
groupRowId in checkGroupMembership: 014-833-26995 InVisiblegroup = 1 11-04 
11:48:22.000: D/MYOB(10145): ContactID in checkGroupMembership: 133 11-04 
11:48:22.030: D/MYOB(10145): groupRowId in checkGroupMembership: 
01483326995 InVisiblegroup = 1 11-04 11:48:22.030: D/DEBUG(10145): 
ContactID in checkGroupMembership: 133 11-04 11:48:22.030: D/DEBUG(10145): 
groupRowId in checkGroupMembership: 10 InVisiblegroup = 1 11-04 
11:48:22.030: D/DEBUG(10145): ContactID in checkGroupMembership: 133 11-04 
11:48:22.030: D/DEBUG(10145): groupRowId in checkGroupMembership: 11 
InVisiblegroup = 1 11-04 11:48:22.030: D/DEBUG(10145): ContactID in 
checkGroupMembership: 133 11-04 11:48:22.030: D/DEBUG(10145): groupRowId in 
checkGroupMembership: 12 InVisiblegroup = 1 11-04 11:48:22.030: 
D/DEBUG(10145): ContactID in checkGroupMembership: 133 11-04 11:48:22.030: 
D/DEBUG(10145): groupRowId in checkGroupMembership: 13 InVisiblegroup = 1 
11-04 11:48:22.030: D/DEBUG(10145): ContactID in checkGroupMembership: 133 
11-04 11:48:22.030: D/DEBUG(10145): groupRowId in checkGroupMembership: 7 
InVisiblegroup = 1 11-04 11:48:22.030: D/DEBUG(10145): ContactID in 
checkGroupMembership: 133 11-04 11:48:22.030: D/DEBUG(10145): groupRowId in 
checkGroupMembership: 8 InVisiblegroup = 1 11-04 11:48:22.030: 
D/DEBUG(10145): ContactID in checkGroupMembership: 133 11-04 11:48:22.030: 
D/DEBUG(10145): groupRowId in checkGroupMembership: 9 InVisiblegroup = 1 
11-04 11:48:22.030: D/DEBUG(10145): ContactID in checkGroupMembership: 133 
11-04 11:48:22.030: D/DEBUG(10145): groupRowId in checkGroupMembership: 
HOME InVisiblegroup = 1 11-04 11:48:22.030: D/DEBUG(10145): ContactID in 
checkGroupMembership: 133

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to