安卓联系人查询

发布于 2024-09-06 20:33:19 字数 424 浏览 6 评论 0原文

我在查询电话簿联系人时遇到问题。我需要做的是获取输入了电话和电子邮件或特定类型的联系人列表。

基本上是这样的:

public static final String SELECTION =
    "("+ContactsContract.Contacts.HAS_PHONE_NUMBER +"='1') OR " + RawContacts.ACCOUNT_TYPE + "='" + Constants.ACCOUNT_TYPE + "'";

现在,问题是,我在查询中使用的 ContactsContract.Contacts.CONTENT_URI 中不存在 RawContacts.ACCOUNT_TYPE 。我猜我需要加入另一张桌子,但不知道该怎么做。

有人可以帮我吗?

I have a problem in querying phonebook contacts. What I need to do is get a list of contacts that have both phone and email entered or are of a specific type.

Basically like this:

public static final String SELECTION =
    "("+ContactsContract.Contacts.HAS_PHONE_NUMBER +"='1') OR " + RawContacts.ACCOUNT_TYPE + "='" + Constants.ACCOUNT_TYPE + "'";

Now, the problem is, that RawContacts.ACCOUNT_TYPE does not exist in the ContactsContract.Contacts.CONTENT_URI, which I use with my query. I'm guessing I'd need to join another table, but have no idea how to do so.

Can anyone help me here, please?

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

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

发布评论

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

评论(2

违心° 2024-09-13 20:33:19

读取原始联系人及其关联的所有数据的最佳方法是使用 ContactsContract.RawContacts.Entity 目录。如果原始联系人具有数据行,则实体游标将为每个数据行包含一行。如果原始联系人没有数据行,则游标仍将包含一行包含原始联系人级别信息。

Uri rawContactUri = ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactId);
 Uri entityUri = Uri.withAppendedPath(rawContactUri, Entity.CONTENT_DIRECTORY);
 Cursor c = getContentResolver().query(entityUri,
          new String[]{RawContacts.SOURCE_ID, Entity.DATA_ID, Entity.MIMETYPE, Entity.DATA1},
          null, null, null);
 try {
     while (c.moveToNext()) {
         String sourceId = c.getString(0);
         if (!c.isNull(1)) {
             String mimeType = c.getString(2);
             String data = c.getString(3);
             //decide here based on mimeType, see comment later
         }
     }
 } finally {
     c.close();
 }

您必须根据 mimeType 过滤结果。

例如,如果 mimeType 为 Phone.CONTENT_ITEM_TYPE,则列 DATA1 存储电话号码,但如果数据类型为是 Email.CONTENT_ITEM_TYPE,则 DATA1 存储电子邮件地址。

这样您就不必使用 HAS_PHONE_NUMBER,因为您将直接迭代项目。

The best way to read a raw contact along with all the data associated with it is by using the ContactsContract.RawContacts.Entity directory. If the raw contact has data rows, the Entity cursor will contain a row for each data row. If the raw contact has no data rows, the cursor will still contain one row with the raw contact-level information.

Uri rawContactUri = ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactId);
 Uri entityUri = Uri.withAppendedPath(rawContactUri, Entity.CONTENT_DIRECTORY);
 Cursor c = getContentResolver().query(entityUri,
          new String[]{RawContacts.SOURCE_ID, Entity.DATA_ID, Entity.MIMETYPE, Entity.DATA1},
          null, null, null);
 try {
     while (c.moveToNext()) {
         String sourceId = c.getString(0);
         if (!c.isNull(1)) {
             String mimeType = c.getString(2);
             String data = c.getString(3);
             //decide here based on mimeType, see comment later
         }
     }
 } finally {
     c.close();
 }

You will have to filter the result based on the mimeType

For example, if the mimeType is Phone.CONTENT_ITEM_TYPE, then the column DATA1 stores the phone number, but if the data kind is Email.CONTENT_ITEM_TYPE, then DATA1 stores the email address.

This way you won't have to use HAS_PHONE_NUMBER as you will directly iterate trough the items.

冰魂雪魄 2024-09-13 20:33:19

也许您应该使用 Email.CONTENT_URI,因为它包含 "vnd.android.cursor.item/email_v2" MIME 类型的所有数据记录,以及关联的原始联系人汇总联系人数据

Maybe you should use Email.CONTENT_URI as it contains all data records of the "vnd.android.cursor.item/email_v2" MIME type, combined with the associated raw contact and aggregate contact data.

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