2

我需要查明某个特定联系人是否是给定组的成员。我一直在使用下面的代码,并证明它可以使用运行 Gingerbread 的手机工作。但是我一直试图在运行 Android 4.0.3 的 HTC One S 上运行它,但它失败了。API 规范有变化吗?我的代码如下。

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

    }

除了更改查询以纯粹找出联系人所属的组之外:

String select = "contact_id=" + contactID;

导致以下调试输出。在某些情况下 groupRowID 看起来像一个组 ID,在其他情况下它是联系人号码或联系人姓名。请注意,我正在使用我的家庭固定电话呼叫测试,并且出现了这种情况。另外 - 我在手机上只定义了 10 个组,并且在输出中打印了 11、12 和 13 的 id ...可能是我遗漏了什么?我绝对知道我传入的 contact_id 是 Coworkers 组的成员(我手机上的 group_ID 为 5),但这从未在 ICS 中返回。我已经使用两部手机上的联系人管理器应用程序独立验证了 HOME 的组成员身份。

11-04 11:48:21.980:D/DEBUG(10145):在 checkGroupMembership 中选择:contact_id=133 11-04 11:48:21.990:D/DEBUG(10145):checkGroupMembership 中的记录数:13 11-04 11 :48:22.000:D / DEBUG(10145):checkGroupMembership中的groupRowId:null InVisiblegroup = 1 11-04 11:48:22.000:D / DEBUG(10145):checkGroupMembership中的ContactID: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) :checkGroupMembership 中的 groupRowId:004-414-83326995 InVisiblegroup = 1 11-04 11:48:22.000:D/DEBUG(10145):checkGroupMembership 中的 ContactID:133 11-04 11:48:22.000:D/DEBUG(10145): checkGroupMembership 中的 groupRowId:014-833-26995 InVisiblegroup = 1 11-04 11:48:22.000:D/MYOB(10145):CheckGroupMembership 中的 ContactID:133 11-04 11:48:22.030:D/MYOB(10145):checkGroupMembership 中的 groupRowId:01483326995 InVisiblegroup = 1 11-04 11:48:22.030:D/DEBUG(10145):checkGroupMembership 中的 ContactID: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) :checkGroupMembership 中的 groupRowId: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 在 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) :checkGroupMembership 中的 ContactID:133 11-04 11:48:22.030:D/DEBUG(10145):checkGroupMembership 中的 groupRowId:9 InVisiblegroup = 1 11-04 11:48:22.030:D/DEBUG(10145):checkGroupMembership 中的 ContactID: 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: 133D/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):检查组成员中的联系人 ID:133D/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):检查组成员中的联系人 ID:133D/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: 133D/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: 133checkGroupMembership 中的 groupRowId:HOME InVisiblegroup = 1 11-04 11:48:22.030:D/DEBUG(10145):checkGroupMembership 中的 ContactID:133checkGroupMembership 中的 groupRowId:HOME InVisiblegroup = 1 11-04 11:48:22.030:D/DEBUG(10145):checkGroupMembership 中的 ContactID:133

PS 请有足够权限的人将“GroupMembership”作为标签添加到网站。

4

2 回答 2

0

Nothing's changed in group membership between Honeycomb and 4.0.3.

It would be easier to read all this if you used LogCat or even did a ListView and screenshot or something. It's hard to understand what your problem is. You can do a dump of ContactsContract.Groups to see what groups are defined for the database. Unless you're absolutely certain that both handsets have exactly the same data, you're gonna find differences. The only way to ensure they have exactly the same data is to wipe the contacts data off them and sync them both to a small test dataset.

于 2012-11-04T19:20:00.647 回答
0

下图显示了 Gingerbread (Cyanogen) 和 ICS (HTC) 使用完全相同的代码生成的组的转储。正如您所看到的,它们在数据的呈现方式方面完全不同。出于某种原因,这些组在 ICS 中在某种程度上是重复的。所以问题是,如果我打算使用组 _ID 来执行查找,我该如何跨不同的版本进行管理。请注意,在 ICS 上的联系人应用程序中,每个组只出现一次,所以我怎么知道这是指哪个 ID?

于 2012-11-08T09:07:43.950 回答