Android - Get All Contacts from All Sources

こ雲淡風輕ζ 提交于 2019-12-06 09:01:14

问题


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!


回答1:


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);
}



回答2:


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");
                  }


来源:https://stackoverflow.com/questions/1987797/android-get-all-contacts-from-all-sources

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!