Android - get all contacts from all sources

I am trying to make an Android application built against 2.0 that requires getting all user contacts and displaying them in a formatted way.

I managed to get the list using the class Cursorand ContactsContract.Contacts. However, the list that I get from this provider only gives me contacts from a Google user account or contacts from two or more sources (for example, Google + Facebook, two Facebook accounts, etc.). This does not give me the whole list.

Those that seem forgotten are primarily those that come only from a Facebook user account and have no other source.

This is a query request that I use:

Cursor contactsCursor = getContentResolver()
    .query(android.provider.ContactsContract.Contacts.CONTENT_URI, 
        null, null, null, null);

My question is, is it possible to get all contacts from each source (Google, Facebook, etc.) in the user's phone book?

Thank!

+3
source share
2 answers

See ContactManager

OBS1: this code uses an obsolete method, managedQuery()you will need to override this part of the code with android.content.CursorLoader.

OBS2: mShowInvisible - if true will display all contacts regardless of user preferences

/**
 * 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);
}
+2
source

I used this code and it is very 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");
                  }
0
source

Source: https://habr.com/ru/post/1727014/


All Articles