使用 Android SDK 获取联系人中的地址

发布于 2024-08-13 05:48:33 字数 160 浏览 15 评论 0原文

我正在尝试从 Android 联系人列表中检索联系人的姓名、电话号码和地址。名称和电话非常简单,但地址似乎无法通过 1.6 api 级别访问。

有人知道如何获取联系人的地址吗? 2.0 中还有一个全新的 api。我如何利用这一点并通过使用 1 个二进制文件回退到旧的 api。如果可能的话。

I'm trying to retrieve the contact's name, phone number, and address from the android contact list. The name and phone are pretty straight forward but the address seems to not be accessible with the 1.6 api level.

Has someone figured out how to get a contact's address? Also there's a completely new api in 2.0. How can I take advantage of this and fallback to the old api by using 1 binary. If that's even possible.

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

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

发布评论

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

评论(4

与他有关 2024-08-20 05:48:33

在 Android 2.0 之前,您需要查询 Contacts.ContactMethods.CONTENT_URI 来获取您需要

Contacts.ContactMethods.PERSON_ID + "=? and " + Contacts.ContactMethods.KIND + "=?"

为选择和 selectArgs 参数传递的邮政地址:

new String[] { personId, Integer.toString(Contacts.KIND_POSTAL) }

从 2.0 开始,您使用新的 ContactsContract,它要复杂得多,因为联系人现在可以汇总多个来源的信息。 此处此处

从同一二进制文件定位多个平台 API 的推荐方法是使用反射(请参阅此 博客文章)。我发现,如果您在清单中将应用程序定位为 1.6 或更低版本,但设置构建路径来获取 Android 2.0 jar 文件,则可以调用 2.0 api,而无需反射。您只需要非常小心,不要在 1.6 设备中加载依赖于 2.0 类的类,否则类加载器将抛出异常。为了避免这种情况,您需要使用某种工厂来打破依赖关系(或像 Guice 这样的 DI 框架)。请参阅此帖子 进行进一步讨论。

Prior to Android 2.0 you need to query Contacts.ContactMethods.CONTENT_URI to get the postal addresses you will need to pass

Contacts.ContactMethods.PERSON_ID + "=? and " + Contacts.ContactMethods.KIND + "=?"

for the selection and for the selectArgs parameter:

new String[] { personId, Integer.toString(Contacts.KIND_POSTAL) }

As of 2.0 you use the new ContactsContract which is far more complex because a contact can now aggregate information across a number of sources. Some good group postings on the new API are here and here.

The recommended way to target multiple platform APIs from the same binary is to use reflection (see this blog post). I have found though if you target your app at 1.6 or below in the manifest but set the build path to pick up the Android 2.0 jar file you can call 2.0 apis without the need for reflection. You just need to be VERY careful that classes that are depended on 2.0 classes are not loaded in a 1.6 device or the class loader will throw. To avoid this you will need to use some sort of factory to break the dependencies (or a DI framework like Guice). See this posting for further discussions.

深居我梦 2024-08-20 05:48:33

感谢 Anthony Forloney(下文)提供的“如何读取联系人”链接,这很有用。要扩展 该链接 的内容,请使用以下代码如何阅读“地址”,这些行应该有帮助!正如您所看到的,他们应该通过使用 StructuredPostal 内容进行查询来获取邮政地址。

            Cursor postals = getContentResolver().query(
                ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_URI,
                null,
                ContactsContract.CommonDataKinds.StructuredPostal.CONTACT_ID + " = "
                        + contactId, null, null);
        int postFormattedNdx = postals.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS);
        int postTypeNdx = postals.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.TYPE);
        int postStreetNdx = postals.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.STREET);
        while (postals.moveToNext()) {
            String postalData = postals.getString(postFormattedNdx);
            postalCat = postalCat+ postalData+ ", [";
            postalData = String.valueOf(postals.getInt(postTypeNdx));
            postalCat = postalCat+ postalData+ "], ";
            postalData = postals.getString(postStreetNdx);
            postalCat = postalCat+ postalData+ " ";
        }
        postals.close();

该链接的其余部分确实获取了名称和电话(当您更正布尔值的“有电话号码”错误时 - 布尔值无法正确解析)

Thanks to Anthony Forloney (below) for the link to "how to read Contacts", that was useful. To extend the content of that link , with the code of how to read "address", these lines should help! They should fetch the postal address, as you see, by querying using the StructuredPostal contants.

            Cursor postals = getContentResolver().query(
                ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_URI,
                null,
                ContactsContract.CommonDataKinds.StructuredPostal.CONTACT_ID + " = "
                        + contactId, null, null);
        int postFormattedNdx = postals.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS);
        int postTypeNdx = postals.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.TYPE);
        int postStreetNdx = postals.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.STREET);
        while (postals.moveToNext()) {
            String postalData = postals.getString(postFormattedNdx);
            postalCat = postalCat+ postalData+ ", [";
            postalData = String.valueOf(postals.getInt(postTypeNdx));
            postalCat = postalCat+ postalData+ "], ";
            postalData = postals.getString(postStreetNdx);
            postalCat = postalCat+ postalData+ " ";
        }
        postals.close();

The rest of that link does indeed fetch the name and the phones (when you correct the "has Phone number" mistake for the boolean value--the boolean does not get parsed properly)

嗫嚅 2024-08-20 05:48:33

在我的手机 ATM 上,但这里是一个包含类似问题的链接给你的。

我希望这可以帮助您或为您指明正确的方向。

编辑:

我已经提到过,我会编辑我原来的答案,至少针对您正在寻找的内容,这可能很晚了,您已经有了答案,但如果您仍在寻找一些答案,这里有更多信息,您需要什么寻找的是 ContactsContract.CommonDataKinds.StructuredPostal 和你可以提取有关地址的具体详细信息。对于使用 Android 2.0 的示例此处显示了一些提取示例来自联系人的信息,也就是说,如果您还没有弄清楚的话。

祝你好运。

On my phone atm, but here is a link with a question similar to yours.

I hope this helps or points you in the right direction.

EDIT:

I had mentioned, I would edit my original answer to at least towards what you were looking for, this is probably wicked late and you have your answer already but here is more information if you were still looking for some, what you need to look for is ContactsContract.CommonDataKinds.StructuredPostal and you can extract specific details regarding address. As for an example using Android 2.0 here shows some examples extracting information from a contact, that is if you haven't figured it out already.

Good luck.

月下客 2024-08-20 05:48:33

这就是我在 Android 2.3.3 上的做法:

            String where = ContactsContract.Data._ID + " = "
                                                     + uriData.getLastPathSegment()
                                                     + " AND ContactsContract.Data.MIMETYPE = '"
                                                     + ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE
                                                     + "'";

            String[] projection = new String[] { StructuredPostal.STREET,
                                                 StructuredPostal.CITY,
                                                 StructuredPostal.REGION,
                                                 StructuredPostal.POSTCODE,
                                                 StructuredPostal.COUNTRY};

            Cursor cursor = getContentResolver().query( ContactsContract.Data.CONTENT_URI,
                                                         projection,
                                                         where,
                                                         null,
                                                         null);
            String strOutput = "";
            if (cursor.moveToFirst())
            {
                strOutput += "\n" + cursor.getString(0);
                strOutput += "\n" + cursor.getString(1);
                strOutput += "\n" + cursor.getString(2);
                strOutput += "\n" + cursor.getString(3);
                strOutput += "\n" + cursor.getString(4);
            } 

            cursor.close();

注意:uriData 是通过捕获查看地址的意图来获取的。
这是在这里解决的:什么是与联系人/联系人卡片中的“查看工作地址”关联的意图吗?

请随意尝试 ContactsContract.Data.CONTACT_ID,而不是 _ID

this is how I did it with Android 2.3.3 :

            String where = ContactsContract.Data._ID + " = "
                                                     + uriData.getLastPathSegment()
                                                     + " AND ContactsContract.Data.MIMETYPE = '"
                                                     + ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE
                                                     + "'";

            String[] projection = new String[] { StructuredPostal.STREET,
                                                 StructuredPostal.CITY,
                                                 StructuredPostal.REGION,
                                                 StructuredPostal.POSTCODE,
                                                 StructuredPostal.COUNTRY};

            Cursor cursor = getContentResolver().query( ContactsContract.Data.CONTENT_URI,
                                                         projection,
                                                         where,
                                                         null,
                                                         null);
            String strOutput = "";
            if (cursor.moveToFirst())
            {
                strOutput += "\n" + cursor.getString(0);
                strOutput += "\n" + cursor.getString(1);
                strOutput += "\n" + cursor.getString(2);
                strOutput += "\n" + cursor.getString(3);
                strOutput += "\n" + cursor.getString(4);
            } 

            cursor.close();

NOTE: the uriData is gotten by capturing the intent to view the address.
This is solved here: What is the intent associated with "View work address" in Contacts / Contact Card?

Instead of _ID, feel free to try ContactsContract.Data.CONTACT_ID

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