title: how to add bulk contacts in android more than 5000?
body: <p>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.</p>
  
  <pre><code>import java.util.ArrayList;
  
  import android.app.IntentService;
  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&lt;ContentProviderOperation&gt; ops;
  
      public MyServices() {
          super("adding Contatcts");
  
      }
  
      @Override
      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&lt;ContentProviderOperation&gt;();
          ops.add(ContentProviderOperation
                  .newInsert(ContactsContract.RawContacts.CONTENT_URI)
                  .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)
                  .withValue(RawContacts.ACCOUNT_NAME, null).build());
          for (int i = 1; i &lt; 220; i++) {
              rawContactID = ops.size();
  
              if (rawContactID % 100 == 1) {
                  doOpration();
                  ops = new ArrayList&lt;ContentProviderOperation&gt;();
              }
  
              String newName = "WhatsAppDating".concat("  " + j);
              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
              ops.add(ContentProviderOperation
                      .newInsert(ContactsContract.Data.CONTENT_URI)
                      .withValueBackReference(
                              ContactsContract.Data.RAW_CONTACT_ID, i)
                      .withValue(ContactsContract.Data.MIMETYPE,
                              StructuredName.CONTENT_ITEM_TYPE)
                      .withValue(StructuredName.DISPLAY_NAME, newName).build());
  
              // Adding insert operation to operations list
              // to insert Mobile Number in the table
              // ContactsContract.Data
              ops.add(ContentProviderOperation
                      .newInsert(ContactsContract.Data.CONTENT_URI)
                      .withValueBackReference(
                              ContactsContract.Data.RAW_CONTACT_ID, i)
                      .withValue(ContactsContract.Data.MIMETYPE,
                              Phone.CONTENT_ITEM_TYPE)
                      .withValue(Phone.NUMBER, fNumber)
                      .withValue(Phone.TYPE, CommonDataKinds.Phone.TYPE_MOBILE)
                      .build());
  
          }
  
      }
  
      private void doOpration() {
          try {
              Log.e("One 100 done", String.valueOf(ops.size()));
              getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
  
          } catch (RemoteException e) {
              e.printStackTrace();
          } catch (OperationApplicationException e) {
              e.printStackTrace();
          }
          Toast.makeText(getApplicationContext(),
                  "Contacts are successfully added", Toast.LENGTH_SHORT).show();
          return;
      }
  
  }
  </code></pre>