gpt4 book ai didi

java - 可以从收到的电话号码获取 ID,但不能从群组获取 ID

转载 作者:行者123 更新时间:2023-12-01 15:36:56 25 4
gpt4 key购买 nike

我已成功从拨入的电话号码中获取 ID 和姓名。我想要查看该 ID 属于哪些组。我尝试过以下方法:

    //Search for the information about the phone number, save the goupID(s)
Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(aNumber));
ContentResolver cr = mService.getContentResolver();
Cursor myCursor = cr.query(uri, new String[]{PhoneLookup._ID, PhoneLookup.DISPLAY_NAME},null, null, null);

myCursor.moveToFirst();
//String contactID = myCursor.getString(myCursor.getColumnIndex(PhoneLookup._ID));
String contactID = myCursor.getString(myCursor.getColumnIndex(ContactsContract.Contacts._ID));
myCursor.close();

//Use the cursor to query for group with help of ID from the Phone look up
myCursor = cr.query(ContactsContract.Groups.CONTENT_URI,
new String[]{ContactsContract.Groups._ID},
ContactsContract.Groups._ID + " = ?",
new String[]{contactID},
null);

//Contact may be in more than one group
nbrOfGroups = myCursor.getCount();
groupName = new String [nbrOfGroups];

问题是第二个查询,我想使用在电话查找中找到的 contactID 来查看 contactID 属于哪些组。结果是没有组,尽管该联系人已添加到我的联系人中的组中。

有什么想法吗? :)

最佳答案

Groups._ID与Contact ID不同,它是存储所有组信息的表的索引。获取联系人 ID 后,您应该使用组成员资格 mimetype 从数据表中获取该联系人的所有组成员资格。

获取组 ID 后,您可以查询组表以获取所有组的标题

尝试使用此代码

    //Search for the information about the phone number, save the goupID(s)
Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode("123123"));
ContentResolver cr = this.getContentResolver();
Cursor myCursor = cr.query(uri, new String[]{PhoneLookup._ID, PhoneLookup.DISPLAY_NAME},null, null, null);

myCursor.moveToFirst();
//String contactID = myCursor.getString(myCursor.getColumnIndex(PhoneLookup._ID));
String contactID = myCursor.getString(myCursor.getColumnIndex(ContactsContract.Contacts._ID));
myCursor.close();

//Use the cursor to query for group with help of contact ID from the Phone look up
myCursor = cr.query(ContactsContract.Data.CONTENT_URI,
new String[]{ContactsContract.Data.CONTACT_ID,
ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID},
ContactsContract.Data.CONTACT_ID + " = ? " +
Data.MIMETYPE + "='" + ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE + "'",
new String[]{contactID},
null);

//Contact may be in more than one group
int nbrOfGroups = myCursor.getCount();
int[] groupIds = new int[nbrOfGroups];
int index = 0;

// unfortunately group names are stored in Groups table
// so we need to query again
if (myCursor.moveToFirst()) {
do {
groupIds[index] = myCursor.getInt(1); // Group_row_id column
} while (myCursor.moveToNext());
}

myCursor.close();

// construct the selection
StringBuffer sb = new StringBuffer();
for (int i = 0; i < nbrOfGroups; i++) {
if (i != 0) {
sb.append(",");
}

sb.append(groupIds[i]);
}

String[] groupName = new String [nbrOfGroups];
myCursor = cr.query(ContactsContract.Groups.CONTENT_URI,
new String[]{ContactsContract.Groups.TITLE},
ContactsContract.Groups._ID + " IN (" + sb.toString() + ")",
null, null);

// finally got the names
if (myCursor.moveToFirst()) {
do {
groupName[index] = myCursor.getString(0); // Group_row_id column
} while (myCursor.moveToNext());
}

myCursor.close();

关于java - 可以从收到的电话号码获取 ID,但不能从群组获取 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8717067/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com