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

发布于 2024-12-23 18:00:13 字数 1214 浏览 2 评论 0原文

我已成功从拨入的电话号码中获取 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 属于哪些组。结果是没有组,尽管该联系人已添加到我的联系人中的组中。

有什么想法吗? :)

I have succeded in getting the ID and name from a phone number that is calling in. What I would like to to is to see which groups this ID belongs to. I have tried the following:

    //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];

The problem is tha second query, where I would like to use the contactID that i found in the phone lookup, to see which groups that contacID belongs to. The result is no group, although the contact is added to a group in my contacs.

Any ideas? :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深海蓝天 2024-12-30 18:00:13

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();

Groups._ID is not same as Contact ID, instead it is the index for the table storing all the group information. After you get the contact ID you should get all the group membership for that contact from the data table by using the Group membership mimetype.

After getting the group ids you can query the Groups table to get the TITLE for all the groups

try with this code

    //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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文