基于 PhoneLookup 获取特定联系人的 RawContactId

发布于 2024-12-29 06:53:09 字数 1340 浏览 2 评论 0原文

我希望使用 PhoneLookup 甚至仅使用 Contacts LookupKey 获取特定联系人RAW_CONTACT_ID

我知道联系人表有一列 name_raw_contact_id 引用了 raw_contacts._id 列,但在查询 ContactsContract.Contacts.CONTENT_LOOKUP_URI< 时似乎没有返回/code> 使用联系人查找键。

我的电话查找查询是:

String[] projection = new String[] { 
    PhoneLookup.DISPLAY_NAME, PhoneLookup.LOOKUP_KEY };
Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
Cursor c = resolver.query(uri, projection, null, null, null);

然后我根据查找键查找 Contact

String[] contactProjection = new String[] { 
    ContactsContract.Contacts.NAME_RAW_CONTACT_ID 
};
Uri contactUri = Uri.withAppendedPath(
    ContactsContract.Contacts.CONTENT_LOOKUP_URI, lookupKey);
Cursor contactCursor = resolver.query(contactUri, 
    contactProjection, null, null, null);

但是,这不会编译,我得到

cannot find symbol: variable NAME_RAW_CONTACT_ID 
location: class android.provider.ContactsContract.Contacts

But the android 文档NAME_RAW_CONTACT_ID 显示为一列。

有什么方法可以根据电话号码或查找键获取 RAW_CONTACT_ID 吗?

I am looking to get the RAW_CONTACT_ID of a specific Contact using PhoneLookup or even just the Contacts LookupKey.

I know the contacts table has a column name_raw_contact_id that references the raw_contacts._id column but it doesn't seem to be returned when querying ContactsContract.Contacts.CONTENT_LOOKUP_URI with the contacts lookup key.

My phone lookup query is:

String[] projection = new String[] { 
    PhoneLookup.DISPLAY_NAME, PhoneLookup.LOOKUP_KEY };
Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
Cursor c = resolver.query(uri, projection, null, null, null);

Then I am looking up the Contact based on the lookup key:

String[] contactProjection = new String[] { 
    ContactsContract.Contacts.NAME_RAW_CONTACT_ID 
};
Uri contactUri = Uri.withAppendedPath(
    ContactsContract.Contacts.CONTENT_LOOKUP_URI, lookupKey);
Cursor contactCursor = resolver.query(contactUri, 
    contactProjection, null, null, null);

However, this doesn't compile and I get

cannot find symbol: variable NAME_RAW_CONTACT_ID 
location: class android.provider.ContactsContract.Contacts

But the android documentation shows NAME_RAW_CONTACT_ID as a column.

Is there any way I can get the RAW_CONTACT_ID based off either phone number or lookup key?

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

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

发布评论

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

评论(3

一萌ing 2025-01-05 06:53:09

我发现答案是进行第三个查询:

long rawContactId = -1;
Cursor c = getContentResolver().query(RawContacts.CONTENT_URI,
    new String[]{RawContacts._ID},
    RawContacts.CONTACT_ID + "=?",
    new String[]{String.valueOf(contactId)}, null);
try {
    if (c.moveToFirst()) {
        rawContactId = c.getLong(0);
    }
} finally {
    c.close();
}

但应该注意的是,每个联系人可以有多个 RawContact,并且上述查询将获取与 contactId 关联的所有 RawContact

I found that the answer is to make a third query:

long rawContactId = -1;
Cursor c = getContentResolver().query(RawContacts.CONTENT_URI,
    new String[]{RawContacts._ID},
    RawContacts.CONTACT_ID + "=?",
    new String[]{String.valueOf(contactId)}, null);
try {
    if (c.moveToFirst()) {
        rawContactId = c.getLong(0);
    }
} finally {
    c.close();
}

But it should be noted that there can be multiple RawContact's per one Contact and the above query will get ALL RawContact's associated with the contactId

×纯※雪 2025-01-05 06:53:09

ContactsContract.Contacts.NAME_RAW_CONTACT_ID 列需要 API 级别 21 或更高版本,因此请确保您使用此版本进行编译。
您还可以在第一个查询中使用 ContactsContract.PhoneLookup._ID 列来获取 Contact_Id,然后使用此 Contact_Id > 在你的第三个查询中,这样你的问题将通过 2 个查询而不是 3 个查询得到解决。

ContactsContract.Contacts.NAME_RAW_CONTACT_ID column needs API level 21 or greater so make sure your are compiling with this version.
Also you can use ContactsContract.PhoneLookup._ID column in your first query to get the Contact_Id and then use this Contact_Id in your 3rd query so your problem will be solved in 2 queries instead of 3.

番薯 2025-01-05 06:53:09
private HashSet<Long> getRawContactIdsForContact(long contactId)
{
    HashSet<Long> ids = new HashSet<Long>();

    Cursor cursor = context.getContentResolver().query(RawContacts.CONTENT_URI,
              new String[]{RawContacts._ID},
              RawContacts.CONTACT_ID + "=?",
              new String[]{String.valueOf(contactId)}, null);

    if (cursor != null && cursor.moveToFirst())
    {
        do
        {
            ids.add(cursor.getLong(0));
        } while (cursor.moveToNext());
        cursor.close();
    }

    return ids;
}
private HashSet<Long> getRawContactIdsForContact(long contactId)
{
    HashSet<Long> ids = new HashSet<Long>();

    Cursor cursor = context.getContentResolver().query(RawContacts.CONTENT_URI,
              new String[]{RawContacts._ID},
              RawContacts.CONTACT_ID + "=?",
              new String[]{String.valueOf(contactId)}, null);

    if (cursor != null && cursor.moveToFirst())
    {
        do
        {
            ids.add(cursor.getLong(0));
        } while (cursor.moveToNext());
        cursor.close();
    }

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