检索联系人的昵称

发布于 2024-09-05 12:35:05 字数 623 浏览 4 评论 0原文

我想从地址簿中获取联系人的昵称。我从他的电话号码开始,查询它并希望得到昵称(又名别名)。

Cursor cur = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.NUMBER + " = " + incomingNumber, null, null);

        if (cur.moveToFirst()) {
            Log.e("saymyname", cur.getString(cur.getColumnIndex(ContactsContract.CommonDataKinds.Nickname.NAME)));
            Log.e("saymyname", cur.getString(cur.getColumnIndex(ContactsContract.CommonDataKinds.Nickname.LABEL)));
        }

日志的输出是传入号码(第一个 Log.e() )和 null(第二个 Log.e() ),但我想获取联系人的昵称!

谢谢 汤姆

I want to get the nickname of a contact from addressbook. I start with his phone number, query it and want the nickname (aka alias) as a result.

Cursor cur = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.NUMBER + " = " + incomingNumber, null, null);

        if (cur.moveToFirst()) {
            Log.e("saymyname", cur.getString(cur.getColumnIndex(ContactsContract.CommonDataKinds.Nickname.NAME)));
            Log.e("saymyname", cur.getString(cur.getColumnIndex(ContactsContract.CommonDataKinds.Nickname.LABEL)));
        }

Output of the logs is the incomingNumber (first Log.e() ) and null (second Log.e() ), but I want to get the contact's nickname!

Thanks
Tom

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

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

发布评论

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

评论(3

橘味果▽酱 2024-09-12 12:35:06

昵称与电话号码保存在不同的表中,您必须查询 ContactsContract.Data.CONTENT_URI

检查我对此的回答 问题

Nickname is held in a different table than the phone numbers, you have to query ContactsContract.Data.CONTENT_URI

Check my answer on this question

非要怀念 2024-09-12 12:35:06

Pentium10 的回答非常有帮助!谢谢!

如果有人需要示例,请查看以下代码:

public String accessContact(String incomingNumber) {
        if (incomingNumber == null || "".equals(incomingNumber)) {
            return "unknown";
        }

        try {
            Cursor cur = context.getContentResolver().query(Phone.CONTENT_URI, new String[] {Phone.DISPLAY_NAME, Phone.TYPE, Phone.CONTACT_ID}, Phone.NUMBER + " = " + incomingNumber, null, null);

            int nameIndex = cur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
            int typeIndex = cur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE);
            int idIndex = cur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID);

            String name;
            String type;
            String id;

            if (cur.moveToFirst()) {
                name = cur.getString(nameIndex);
                type = cur.getString(typeIndex);
                id = cur.getString(idIndex);
            } else {
                return "unknown";
            }

            cur = context.getContentResolver().query(ContactsContract.Data.CONTENT_URI, new String[] {ContactsContract.Data.DATA1}, ContactsContract.Data.CONTACT_ID + " = " + id, null, null);

            int nicknameIndex = cur.getColumnIndex(ContactsContract.Data.DATA1);
            String nickname;

            if (cur.moveToFirst()) {
                nickname = cur.getString(nicknameIndex);
                if (nickname.equals(incomingNumber)) {
                    return name;
                }
                return nickname;
            } else {
                return name;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "unknown";
        }

The answer from Pentium10 was very helpful! Thanks!

If anybody needs a sample, look at the following code:

public String accessContact(String incomingNumber) {
        if (incomingNumber == null || "".equals(incomingNumber)) {
            return "unknown";
        }

        try {
            Cursor cur = context.getContentResolver().query(Phone.CONTENT_URI, new String[] {Phone.DISPLAY_NAME, Phone.TYPE, Phone.CONTACT_ID}, Phone.NUMBER + " = " + incomingNumber, null, null);

            int nameIndex = cur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
            int typeIndex = cur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE);
            int idIndex = cur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID);

            String name;
            String type;
            String id;

            if (cur.moveToFirst()) {
                name = cur.getString(nameIndex);
                type = cur.getString(typeIndex);
                id = cur.getString(idIndex);
            } else {
                return "unknown";
            }

            cur = context.getContentResolver().query(ContactsContract.Data.CONTENT_URI, new String[] {ContactsContract.Data.DATA1}, ContactsContract.Data.CONTACT_ID + " = " + id, null, null);

            int nicknameIndex = cur.getColumnIndex(ContactsContract.Data.DATA1);
            String nickname;

            if (cur.moveToFirst()) {
                nickname = cur.getString(nicknameIndex);
                if (nickname.equals(incomingNumber)) {
                    return name;
                }
                return nickname;
            } else {
                return name;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "unknown";
        }
紫瑟鸿黎 2024-09-12 12:35:06

(我没有必要的声誉来发表评论,所以我必须添加答案)

TomTasche 的答案具有误导性,它让我我浪费了很多时间试图弄清楚为什么我无法为我知道有昵称的联系人提供正确的昵称。

我自己找到了答案,但从这篇文章得到了确认,我现在做得正确。

基本上,当您阅读 ContactsContract.Data 文档时,您会阅读:

数据表的每一行通常用于存储一条联系信息(例如电话号码)及其关联的元数据(例如是工作号码还是家庭号码) .

这解释了 TomTasche 代码的可疑部分:

if (nickname.equals(incomingNumber)) {
                return name;
            }

由于仅使用 CONTACT_ID 进行搜索可以返回多行(每种信息类型各一行),因此不能保证第一行包含昵称。当有昵称时,它将位于 DATA1 中,但 电话number 也在 DATA1 中找到。

ContactsContract.Data 文档中的示例部分使得它明确指出必须根据 MIME_TYPE 选择行,只有选择 MIME_TYPE 后,我们才能开始理解行本身的内容。

因此,正确的查询是:(

cursor =  getContentResolver().query(
                ContactsContract.Data.CONTENT_URI,
                new String[]{Nickname.NAME},
                ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + "= ?",
                new String[]{contactID, Nickname.CONTENT_ITEM_TYPE},
                null);

其中昵称是 ContactsContract.CommonDataKinds .昵称

我觉得我必须就这个主题说点什么,以防止其他人像我一样浪费时间在这个唯一的主题上(我和我的谷歌朋友发现的第一个主题)。

(I don't have necessary reputation to comment so I have to add answer)

TomTasche's answer is misleading and it got me to waste a lot of time trying to figure out why I couldn't get the proper nickname on a contact I knew had one.

I found the answer by myself but got the confirmation from this post that I was now doing it properly.

Basically when you read the ContactsContract.Data documentation you read:

Each row of the data table is typically used to store a single piece of contact information (such as a phone number) and its associated metadata (such as whether it is a work or home number).

That explains the shady part of TomTasche's code :

if (nickname.equals(incomingNumber)) {
                return name;
            }

Since doing a search with just the CONTACT_ID can return multiple rows (one for each information type), it's not guaranteed that the first one contains the nickname. When there is a nickname it will be in DATA1, but the phone number is also found in DATA1.

The example part in the documentation of ContactsContract.Data makes it clear that rows must be selected based on their MIME_TYPE, only when the MIME_TYPE is selected, can we start making sense of the content in the row itself.

A proper query would therefore be:

cursor =  getContentResolver().query(
                ContactsContract.Data.CONTENT_URI,
                new String[]{Nickname.NAME},
                ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + "= ?",
                new String[]{contactID, Nickname.CONTENT_ITEM_TYPE},
                null);

(where Nickname is ContactsContract.CommonDataKinds.Nickname)

I felt I had to say something on this topic to prevent other people wasting as much time as I did based on this sole topic (first one I found with my Google friend).

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