I have been attempting to make an Android application, built against 2.0, that requires getting all of the user's contacts and displaying them in a formatted way.
I have been able to get a list using a Cursor
and the ContactsContract.Contacts
class. However, the list I get from that provider only gives me the contacts that are either from the user's Google account, or contacts that have two or more sources (e.g. Google + Facebook, Two Facebook accounts, etc). It does not give me the entire list.
Those that seem left out are primarily those that come only from the user's Facebook account, and have no other source.
This is the query call I'm using:
Cursor contactsCursor = getContentResolver()
.query(android.provider.ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
My question is is it possible to get all the contacts from every single source (Google, Facebook, etc) in the user's phonebook?
Thanks!
See ContactManager
OBS1: this code is using an deprecated method, managedQuery()
you will need to reimplement this part of the code using a android.content.CursorLoader
.
OBS2: mShowInvisible
- if true will list all contacts regardless of user preference
/**
* Obtains the contact list for the currently selected account.
*
* @return A cursor for for accessing the contact list.
*/
private Cursor getContacts(){
// Run query
Uri uri = ContactsContract.Contacts.CONTENT_URI;
String[] projection = new String[] {
ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME
};
String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '"(mShowInvisible ? "0" : "1") + "'";
String[] selectionArgs = null;
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
return managedQuery(uri, projection, selection, selectionArgs, sortOrder);
}
I used this code and it's pretty good.
ContentResolver cr = getContentResolver();
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
all_contacts_nu = cursor.getCount();
if(cursor!=null&&cursor.getCount()>0)
{
cursor.moveToFirst();
Log.i("MAHDI", "cursor.getCount()="+cursor.getCount());
for(int i =0;i<cursor.getCount();i++)
{
counter++;
FileDisplayActivity.this.get(cursor);
cursor.moveToNext();
writeToFile(vCard.get(i));
}
}
else
{
Log.d("TAG", "No Contacts in Your Phone");
}