I am developing an application in which i am able to read the contacts. I refereed this Link.
My code to read contact and basic information is as follow:
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
while (phones.moveToNext()) {
String name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
String contactID = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));
}
phones.close();
Now I want to get the following things from contact as :
1.Type of contact as it is of Device, sim, google, face book etc contact.
2.The number of email for particular contact.
3.Total account(Face book, whats app, skype etc) count for the contact.
4.Total Event(Birthday, anniversary etc) count.
5.The contact is favorite or not
6.The contact is in any device group(Family, Friends) and yes then which groups?
I searched on google but not able to do the desired task.
Please suggest me what should i do.
Related
I am developing an android application and I need to know all the information about phone contacts.
I developed a function to get the name and number of all the contacts, but I need all the information about particular contact such as email, date, favorite or not, image, social links if available.
I got id, name and number from following:
String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String number = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
I used ContactsContract.Contacts to get _ID and DISPLAY_NAME, but
ContactsContract.CommenDataKinds.Phone to get the NUMBER. Is it correct?
Please explain the difference between the two methods.
Is the _ID a unique ID for all the contacts?
After a long discussion with #pskink I finally found the solution to list all related information for each contact in the directory.
First of all, create a cursor:
Cursor cursor = cr.query(ContactsContract.Data.CONTENT_URI, null, null, null, null);
And after that, you can dumb the cursor to show all the informations and see each contact and keywords it needs to use, like (custom_ringtone, display_name, photo_uri, is_primary, ..) by using this line of code:
DatabaseUtils.dumpCursor(cursor);
Special thanks to #pskink
I want to retrive all the contact from phone book that has facebook account.
Steps i did
I got contact id from Contacts.
Based on id get Accounts from RawContact table.
All account associated with contact found but facebook account not found.
You can follow below algorithm to get contacts with specific account type.
1. First, query on raw contacts table and find all raw contacts. Check what are different Account type and Account name are available. http://developer.android.com/reference/android/provider/ContactsContract.SyncColumns.html#ACCOUNT_TYPE
Facebook contact will have account type with text "facebook" in it.
2. Once you got exact account type, you can query on raw contacts with account type = facebook.
3. This will give all Facebook raw contacts. Raw contact table has contact_id field using which you can query on Contacts table to get all Facebook contacts.
Below is the code snippet which can print all account names and types which are currently configured on device for which contacts are present. This is just Sample code to check what all account types are there. If you don't find any account name/type related to facebook, I guess there are no facebook contacts present.
String[] projection = { RawContacts._ID, RawContacts.ACCOUNT_TYPE,
RawContacts.ACCOUNT_NAME };
Cursor cur = cr.query(RawContacts.CONTENT_URI, projection, null, null,
null);
if (cur != null) {
while (cur.moveToNext()) {
long rawContactId = cur.getLong(cur
.getColumnIndexOrThrow(RawContacts._ID));
String accountType = cur.getString(cur
.getColumnIndexOrThrow(RawContacts.ACCOUNT_TYPE));
String accountName = cur.getString(cur
.getColumnIndexOrThrow(RawContacts.ACCOUNT_NAME));
}
}
I have created an application for ContactsContract... I have created a spinner which brings all the accounts configured and hence the user can pick up the contact type say, gmail(com.google), phoneBook and so on....
Now, If I select phoneBook, then the contact gets added in the phoneBook perfectly.
When I select gmail option, It works perfectly on my htc cell phone... The contact gets added, and after sync, I can see that in my gmail account too.
But, the same thing when I test on any of the samsung cell phones, it does not get added to the contacts of my gmail....
I am confused...
Any help is appreciated. Thanks in advance...
Phone numbers are stored in their own table and need to be queried separately. To query the phone number table use the URI stored in the SDK variable ContactsContract.CommonDataKinds.Phone.CONTENT_URI. Use a WHERE conditional to get the phone numbers for the specified contact.
if (Integer.parseInt(cur.getString(
cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
Cursor pCur = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
new String[]{id}, null);
while (pCur.moveToNext()) {
// Do something with phones
}
pCur.close();
}
Perform a second query against the Android contacts SQLite database. The phone numbers are queried against the URI stored in ContactsContract.CommonDataKinds.Phone.CONTENT_URI. The contact ID is stored in the phone table as ContactsContract.CommonDataKinds.Phone.CONTACT_ID and the WHERE clause is used to limit the data returned.
Email Addresses
Querying email addresses is similar to phone numbers. A query must be performed to get email addresses from the database. Query the URI stored in ContactsContract.CommonDataKinds.Email.CONTENT_URI to query the email address table.
Cursor emailCur = cr.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
new String[]{id}, null);
while (emailCur.moveToNext()) {
// This would allow you get several email addresses
// if the email addresses were stored in an array
String email = emailCur.getString(
emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
String emailType = emailCur.getString(
emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
}
emailCur.close();
I have an app which can retrieve the mailing address from contact information on the device. I retrieve the information for street address, post code, city etc. using the contacts API using code similar to below. I then display the address on separate lines and the user can accept it or change it if desired.
String where = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
String[] params = new String[]{id, ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE};
Cursor addrCur = getContentResolver().query(ContactsContract.Data.CONTENT_URI, null, where, params, null);
...
while(addrCur.moveToNext()) {
String street = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.STREET));
String city = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.CITY));
String state = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.REGION));
String postalCode = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE));
String country = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY));
...
}
addrCur.close();
This has worked fine with Android 2.2/2.3 but I noticed on 3.x and higher that the entire address that is returned can be formatted as a single string with line breaks in the STREET field. This occurs when the contact/address is created on the device; if I sync a contact from Gmail, the address is retrieved correctly (I suspect that the synced info is saved in the correct fields). I can't see any way to get the individual data elements (street, postcode, etc) for these types of addresses. Is this possible? Is there a new API I need to use?
I haven't found a way to do it. The only workaround I've found is to get the full, formatted address by getting the ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS column from the cursor and then attempt to break it apart assuming a couple different standard formattings. There's no way to input an address in the Honeycomb/ICS Contacts app other than using a free-form text box, so I guess the Contacts app doesn't try and parse it automatically before storing it in the database.
Hopefully I'm wrong and there is a way to do it - but I haven't found it yet.
Is it possible to read more than one contact from the contact list from my app.??
what i found is how to receive a list of contacts and loop through the list.
How to read contacts on Android 2.0
but is it possible to select a few of them from the list of contacts.do i need to create a seperate layout for selecting the contacts of my choice and load that layout with data from the contacts list?? please help.
Obviously you are able to select contacts of your choice from list of contacts.But you just need to find a proper "where" clause in query(i.e. what type of contacts you want from list).
And yes,you need to create separate layout to show them up.
For Example: Getting contacts with phone number
Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null, ContactsContract.Contacts.HAS_PHONE_NUMBER+"='true'", null, null); // gives you the list of contacts who has phone numbers
while (cursor.moveToNext()) {
String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
Cursor phones = getContentResolver().query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId, null, null);
while (phones.moveToNext()) {
String phoneNumber = phones.getString(phones.getColumnIndex( ContactsContract.CommonDataKinds.Phone.NUMBER));
}
phones.close();
}
Hope,i get your requirement correctly.If not,please let me know!
Basically you need to iterate through all contacts with their unique
id called contact_id.
Avoid using cursors inside cursors in the loop.
try this demo app. see how quickly it reads contacts in a proper android way
Source code attached