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.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<ContentProviderOperation> 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<ContentProviderOperation>();
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 < 220; i++) {
rawContactID = ops.size();
if (rawContactID % 100 == 1) {
doOpration();
ops = new ArrayList<ContentProviderOperation>();
}
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;
}
}
Aucun commentaire:
Enregistrer un commentaire