基于 PhoneLookup 获取特定联系人的 RawContactId
我希望使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我发现答案是进行第三个查询:
但应该注意的是,每个联系人可以有多个 RawContact,并且上述查询将获取与
contactId
关联的所有 RawContactI found that the answer is to make a third query:
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
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.