联系人按电话号码选择合同记录 [英] ContactsContract select records by phone number
问题描述
我可以使用下面的查询选择所有联系人
I can select all contacts using the query below
cr = mActivity.getContentResolver();
String selection = ContactsContract.Contacts.HAS_PHONE_NUMBER + " > 0";
String orderBy = ContactsContract.Contacts.DISPLAY_NAME + " ASC ";
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, selection, null, orderBy);
但是,我有一个电话号码列表,我想创建类似
However, I have a phone number list and I want to create this query like
String selection = ContactsContract.Contacts.PhoneNumber_or_something_else in (MyPhoneNumberArray)
可以这样做吗?
在最坏的情况下,我可以在创建游标后使用do删除相关项,但是据我所知,我无法从游标中删除任何记录.
A worst-case scenario, I can remove the related item using do while after I create my cursor, but, as far as I know, I cannot remove any record from the cursor.
推荐答案
Contacts DB分为三个主要表:
The Contacts DB is organized in three main tables:
-
Contacts
-每个条目代表一个联系人,并将一个或多个RawContacts
组合在一起
-
RawContacts
-每个条目代表有关由某些SyncAdapter
(例如Whatsapp,Google,Facebook,Viber)同步的联系人的数据,这会将多个Data条目分组 -
Data
-有关联系人,电子邮件,电话等的实际数据.每行是属于单个RawContact
的单个数据
Contacts
- each entry represents one contact, and groups together one or moreRawContacts
RawContacts
- each entry represents data about a contact that was synced in by someSyncAdapter
(e.g. Whatsapp, Google, Facebook, Viber), this groups multiple Data entriesData
- The actual data about a contact, emails, phones, etc. each line is a single piece of data that belongs to a singleRawContact
Contacts数据库中的所有电话号码都在Data
表中,因此这就是您要查询的内容,您可以从该查询中获取CONTACT_ID
的列表,并使用它来获取有关联系人的常规信息.需要.
All phone numbers in the Contacts DB are in the Data
table, so that's what you need to query, you can get the list of CONTACT_ID
s from that query and use it to get general info about contacts if you need.
String[] phonesList = new String[] { "+121212345" }; // will work better if all phones in this list are in e164 format
String[] projection = { Phone.CONTACT_ID, Phone.DISPLAY_NAME, Phone.NUMBER, Phone.NORMALIZED_NUMBER };
String selection = Phone.NUMBER + " IN ('" + TextUtils.join("','", phonesList) + "') OR " +
Phone.NORMALIZED_NUMBER + " IN ('" + TextUtils.join("','", phonesList) + "')";
Cursor cur = cr.query(Phone.CONTENT_URI, projection, selection, null, null);
while (cur != null && cur.moveToNext()) {
long id = cur.getLong(0);
String name = cur.getString(1);
String phone = cur.getString(2);
Log.d(TAG, "got " + id + ", " + name + ", " + phone;
}
这篇关于联系人按电话号码选择合同记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!