I made an App that Sync contacts from android contacts repository as follows : when you finish the sign up process It asks you fro syncing your contacts like Facebook does and then it sends all your contacts phone numbers to Server and the servers returns if your contacts use the same app if not it asks you to invite them.
All works fine till one of my clients who do have 30 000 contacts made me ask here for a solution, because of the 30 000 contacts the sync process doesn't finish and he can't even access his repository because my app and Android System runs in concurrency. Thanks for helping in Advance
The code below is the one I use : it returns a List containing all the contacts then I run in a for loop to get all contacts photos to show them and I send the List to the server to check if they use the app.
public class Phoner {
public static void loadPhoneContacts(Context context,
final OnPhoneContactsLoadedListener listener) {
final List<Bundle> phoneContacts = new ArrayList<Bundle>();
final String[] PROJECTION = new String[] { ContactsContract.Data._ID,
ContactsContract.Data.DISPLAY_NAME,
ContactsContract.Data.PHOTO_URI,
ContactsContract.Data.LOOKUP_KEY,
ContactsContract.Contacts.HAS_PHONE_NUMBER,
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Im.DATA };
final String SELECTION = "(" + ContactsContract.Data.MIMETYPE + "=\""
+ ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE
+ "\");
CursorLoader mCursorLoader = new CursorLoader(context,
ContactsContract.Data.CONTENT_URI, PROJECTION, SELECTION, null,
null);
mCursorLoader.registerListener(0, new OnLoadCompleteListener<Cursor>() {
@Override
public void onLoadComplete(Loader<Cursor> arg0, Cursor cursor) {
if (cursor == null) {
return;
}
while (cursor.moveToNext()) {
Bundle contact = new Bundle();
contact.putInt("phoneid", cursor.getInt(cursor
.getColumnIndex(ContactsContract.Data._ID)));
contact.putString(
"displayname",
cursor.getString(cursor
.getColumnIndex(ContactsContract.Data.DISPLAY_NAME)));
contact.putString("photouri", cursor.getString(cursor
.getColumnIndex(ContactsContract.Data.PHOTO_URI)));
contact.putString("lookup", cursor.getString(cursor
.getColumnIndex(ContactsContract.Data.LOOKUP_KEY)));
phoneContacts.add(contact);
the for loop is as follows: This code is inside a service
private void loadPhoneContacts() {
Phoner.loadPhoneContacts(getApplicationContext(),
new OnPhoneContactsLoadedListener() {
@Override
public void onPhoneContactsLoaded(List<Bundle> phoneContacts) {
for (Bundle phoneContact : phoneContacts) {
for (Account account : accounts) {
phoneContact
.getString("photouri");
phoneContact
.getString("displayname");
}
}
}
});
}
P.s: I've heard of the sync Adapter but I don't know if it can leverage that much amount of contacts. Thanks