how to add bulk contacts in android more than 5000?

I am trying to add multiple contacts in android for that i used a service and so that my UI would not get effected. but problem is that i am getting an error that yield point cant accept more than 500 at a time. now i am trying it in chunks still i am not able to add more than 120 contacts in one GO!!. please reply.

import java.util.ArrayList;

import android.content.ContentProviderOperation;
import android.content.Intent;
import android.content.OperationApplicationException;
import android.os.RemoteException;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds;
import android.provider.ContactsContract.RawContacts;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.CommonDataKinds.StructuredName;
import android.util.Log;
import android.widget.Toast;

public class MyServices extends IntentService {

	long mNumber;
	String cMobile;
	static int rawContactID;
	public static String CC = "countrycode";
	public static String PHONENUMBER = "";
	ArrayList<ContentProviderOperation> ops;

	public MyServices() {
		super("adding Contatcts");


	protected void onHandleIntent(Intent intent) {
		// String cc = intent.getStringExtra(CC);
		String cNumber = intent.getStringExtra(PHONENUMBER);
		// Log.e("CC", String.valueOf(cc));

		try {

			mNumber = Long.parseLong(cNumber);

		} catch (Exception e) {


		int j = 1001;
		ops = new ArrayList<ContentProviderOperation>();
				.withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)
				.withValue(RawContacts.ACCOUNT_NAME, null).build());
		for (int i = 1; i < 220; i++) {
			rawContactID = ops.size();

			if (rawContactID % 100 == 1) {
				ops = new ArrayList<ContentProviderOperation>();

			String newName = "WhatsAppDating".concat("  " + j);
			long k = mNumber;
			long m = k + i;
			Log.e("adding", String.valueOf(m));
			String fNumber = String.valueOf("+91" + m);

			// Adding insert operation to operations list
			// to insert display name in the table ContactsContract.Data
							ContactsContract.Data.RAW_CONTACT_ID, i)
					.withValue(StructuredName.DISPLAY_NAME, newName).build());

			// Adding insert operation to operations list
			// to insert Mobile Number in the table
			// ContactsContract.Data
							ContactsContract.Data.RAW_CONTACT_ID, i)
					.withValue(Phone.NUMBER, fNumber)
					.withValue(Phone.TYPE, CommonDataKinds.Phone.TYPE_MOBILE)



	private void doOpration() {
		try {
			Log.e("One 100 done", String.valueOf(ops.size()));
			getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);

		} catch (RemoteException e) {
		} catch (OperationApplicationException e) {
				"Contacts are successfully added", Toast.LENGTH_SHORT).show();


Source: android

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.