我需要查明某个特定联系人是否是给定组的成员。我一直在使用下面的代码,并证明它可以使用运行 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”作为标签添加到网站。