如何在Android 2.0上读取联系人
我正在开发 Android 2.0,并试图接收所有联系人的列表。
由于android.provider.Contacts.People已被弃用,我必须使用android.provider.ContactsContract,但我找不到如何使用它的正确示例(例如:检索电话簿上所有联系人的列表)。
有人知道在哪里可以找到这样的例子吗?
I'm working on Android 2.0 and am trying to receive a list of all contacts.
Since android.provider.Contacts.People is deprecated, I have to use android.provider.ContactsContract, But I can't find a proper example of how to use it (ex: retrieve a list of all contacts on the phonebook).
Anyone knows where to find such an example?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
首先,确保您已添加
到 AndroidManifest.xml 文件中,然后您可以像这样循环遍历您的手机联系人:
此外,您可以循环遍历您的联系人并简单地获取姓名和电话号码,如下所示:
此外,如果您需要从联系人处获取注释之类的内容,那么您将需要使用不同的 URI,如下所示(随意使用此方法):
请注意,这次我不仅使用联系人 ID,还使用 MIME 类型进行查询。
First, ensure that you have added
to your AndroidManifest.xml file, then you can loop through your phone contacts like this:
Additionally, you can loop through your contacts and simply get the name and phone number like this:
Furthermore, if you need to get things like notes from a contact then you will need to use a different URI, like the following (feel free to use this method):
Notice this time I used not only the contact id but the mime type for the query.
很高兴看到一些有用的信息,但令人沮丧的是,文档等内容对这个重要主题的描述非常糟糕。经过太多的黑客攻击之后,我想我也应该分享一些代码。下面的代码更漂亮一些,并且可以更有效地完成相同的事情。
上面的代码块返回一个游标,该游标指向仅包含具有电话号码的行的结果查询。这很好,因为您通常有很多没有号码的联系人。此外,投影还限制了返回的数据量。
上面的块获取与每个具有电话号码的联系人 ID 关联的电话号码。我将所有信息存储在哈希表中,并以电话号码为键值。我还删除了电话号码中的所有非数字信息。由于某种原因,即使 ContactsContract.CommonDataKinds.Phone.HAS_PHONE_NUMBER 是有效的,如果您将其包含在投影参数中,它也会破坏查询,但我不知道为什么,而且它确实如此,这令人沮丧。
上面代码的第二部分太慢了,所有的查询调用都会让一切陷入困境。下面的代码要快得多。只需抓取电话内容的所有行并使用 contact_ids 对所需的数据进行排序即可。
您最终会得到一个哈希表,其中包含您想要的所有信息。当然,您可以将任何您想要的信息放入数据结构中。第二种方法要快得多。
Great to see some useful info, it is frustrating how poorly this important topic is covered by docs and such. After too much hacking about I thought I would share a little code also. The following code is a little prettier and get the same thing done more efficiently.
The above chunk of code returns a Cursor that points to the resulting query that only contains those rows that have a phone number. This is nice since you typically have many contacts without numbers. Furthermore, the PROJECTION limits the amount of data that is returned.
The above chunk gets the phone number associated with each contact id that has a phone number. I store all the info in a hash table and with a key value of the phone number. I stripped the phone number of all none digit info also. For some reason even though ContactsContract.CommonDataKinds.Phone.HAS_PHONE_NUMBER is valid if you include that in the projection argument it breaks the query, I don't know why and it is frustrating that it does.
The second part of the code above is too slow, all the query calls just bog everything down. The following code is much faster. Just grab all the rows for the phone content and use the contact_ids to sort the data you want.
You end up with a hashtable with all the info you want in it. Of course you could put whatever info you want into the data structure. The second way of doing it is much much faster.
只是想补充一点,当您检索联系人时,您可能会得到很多“垃圾”联系人 - 例如,用户在某个时刻向其发送电子邮件但未聚合的某些电子邮件地址...如果您只想用户可见的联系人,就像在 Android 自己的联系人应用程序中一样,您需要将选择限制为仅 IN_VISIBLE_GROUP。
Just want to add, when you are retrieving the contacts you might get a lot of "garbage" contacts - for example some email addresses that a user has at some point send an email to, but are not aggregated... If you want only the contacts visible to the user, as in the Androids own contacts application you need to restrict the selection to only IN_VISIBLE_GROUP.
我认为从这个 URL 获取代码非常重要StackOverflow 上的 http://coderzheaven.com/2011/06/get-all-details-from-contacts-in-android/ 有时会导致此类链接失效。
强调文字
I think it is important to have the code from this URL http://coderzheaven.com/2011/06/get-all-details-from-contacts-in-android/ on StackOverflow cause at times links like that go down.
emphasized text
我找到了读取联系人的非常简单的解决方案。 (编写读取每个值的代码很无聊,因此最好使用联系人包装类)
当然
ContactList.java
Contact.java
Address.java
Email.java
Im.java
Organization.java
Phone.java
ContactAPI.java
ContactAPISdk5.java
ContactAPISdk3.java
注意 :不要忘记更改包名称
** *****
。来源(链接随时可能失效:))
I found very easy solution to read contacts. (boring to write code for reading each value so it's good to use wrapper class for contacts)
Of course
<uses-permission android:name="android.permission.READ_CONTACTS"/>
ContactList.java
Contact.java
Address.java
Email.java
Im.java
Organization.java
Phone.java
ContactAPI.java
ContactAPISdk5.java
ContactAPISdk3.java
Note : Don't forget to change package name instead
*******
.Source (link can be die any time :))
把这个...
如果有任何问题请告诉我。
Put this ....
Let me know if any issue.
这部分对我不起作用:
但是,如果我使用它,它会:
This part wouldn't work for me:
If I use this, though, it does:
您可以使用 Android 开发者网站上的“ContactManager”示例
(或者)
转到您在系统中设置的下载 android-sdk 路径的位置。在 android-sdk-mac_x86/samples/android-10 文件夹中,您可以看到“ContactManager”示例。
我尝试过使用这个例子,在我的应用程序中运行良好。
You can use "ContactManager" example from android developer's site
(OR)
Go to the location where you have set the path to download android-sdk in your system. In android-sdk-mac_x86/samples/android-10 folder, you can see "ContactManager" example.
I have tried using this example, worked well in my application.
我正在使用三星 Galaxy Note 4,但我不知道为什么上述方法都不适合我。
我混合了一些并做了这个 woking..
I'm using Samsung Galaxy Note 4, and I donno why none of the above worked for me.
I mixed up some and made this woking..