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

2023-09-05 05:16:10 作者:划不完美的和弦

我想从Android联系人列表中检索联系人的姓名,电话号码和地址。这个名字和电话是pretty的直线前进,但地址似乎无法与1.6 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.

有一个人想出如何获取联系人的地址?另外还有2.0一个完全新的API。我该如何利用这一点,并用1的二进制退回到旧的API。如果这是有可能的。

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.

推荐答案

此前Android 2.0的,你需要查询Contacts.ContactMethods.CONTENT_URI得到邮政地址,您将需要通过

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 + "=?"

选择和为selectArgs参数:

for the selection and for the selectArgs parameter:

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

作为2.0您使用新ContactsContract远远更加复杂,因为一个接触可以跨越若干来源现在聚集信息。在新的API有一些很好的组帖子是这里和这里。

推荐的方式将来自同一个二进制多平台API是使用反射(见本的博客文章)。我发现,但如果你的目标你的应用程序在1.6或以下的清单,但设置构建路径,搭载了Android 2.0的jar文件就可以调用2.0 API的,而不需要进行反思。你只需要非常小心,这都依赖于2.0类类1.6设备都没有加载或类加载器将抛出。为了避免这种情况,你将需要使用工厂的某种打破依赖关系(或DI框架,比如吉斯)。看到这个发布进一步讨论。

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.