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?
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.
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..
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 :))
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
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.
Put this ....
Let me know if any issue.