Bulk update of more than 500 contacts
You're not creating a new object for ops
. During subsequent calls to applyBatch
, you're passing the previously applied operations back in as well. The first time ops
contains 100 elements, then 200 and eventually it fails when it reaches 500. Change to
if (i % batchsize == 0) {
contentResolver.applyBatch(ContactsContract.AUTHORITY, ops);
ops = new ArrayList<ContentProviderOperation>(100);
}
on contact insert get Contact_ID
Better way, and you get the _ID not the RAW_ID!!
Add that as static variables:
static final String[] CONTACTS_SUMMARY_PROJECTION = new String[] {
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts.STARRED,
ContactsContract.Contacts.TIMES_CONTACTED,
ContactsContract.Contacts.CONTACT_PRESENCE,
ContactsContract.Contacts.PHOTO_ID,
ContactsContract.Contacts.LOOKUP_KEY,
ContactsContract.Contacts.HAS_PHONE_NUMBER,
};
Then add that after the insert:
// Displayed name
String displayed_name = "Contact's name goes here";
// Add the new contact
try
{
context.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
}
catch (Exception e)
{
e.printStackTrace();
}
String select = "(" + ContactsContract.Contacts.DISPLAY_NAME + " == \"" +displayed_name+ "\" )";
Cursor c = context.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, CONTACTS_SUMMARY_PROJECTION, select, null, ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
context.startManagingCursor(c);
if (c.moveToNext())
{
int id = new Integer(c.getString(0)).intValue();
}
It works for me.
Multiple rows insert with ContentProvider
On the client side, ContentResolver
supports a bulkInsert()
method. Those will not necessarily be processed in a single transaction by the ContentProvider
, simply because there may not be any transactions performed by the ContentProvider
.
Most Efficient Way to Insert 5000+ Android Contacts
Unfortunately, I believe 1 is the best option. I suspect a majority of your overhead in comparison to iPhone is in the cross process IPC inherent to the content provider design.
Your analysis of 3 is correct.
There are options on rooted devices to go around the content provider but I doubt that is what you are looking for.
Related Topics
How to Create Converter for My Class in Android Retrofit Library
Android Application Class Lifecycle
Assign Width to Half Available Screen Width Declaratively
Cordova Plugin Development - Adding Aar
Android: How to Get Gsm Signal Strength for All Available Network Operators
React Native Adb Reverse Enoent
How to Use Bluetooth in Android Emulator
How to Do Something After User Clicks on My Edittext
Mkdir() Works While Inside Internal Flash Storage, But Not Sd Card
Android 1.5: Reading Sms Messages
How to Remove Shared Preference While Application Uninstall in Android
How to Notify Users About an Android App Update
How to Use Isineditmode() to See Layout with Custom View in the Editor
Extended Surfaceview's Ondraw() Method Never Called
Voice Recognition Stops Listening After a Few Seconds
Adding Watermark Bitmap Over Video in Android: 4.3's Mediamuxer or Ffmpeg