Попробуйте, как показано ниже:
private void addContacts(){
try
{
Cursor c = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
if (c != null) {
while (c.moveToNext()) {
if (c.getString(c.getColumnIndex(Phone.DISPLAY_NAME)).equalsIgnoreCase(officeText.getText().toString())) {
Log.d("ContactUSActivity", "Number Exist");
Toast.makeText(ContactUsActivity_2_0.this,getResources().getString(R.string.contact_already_exist),Toast.LENGTH_SHORT).show();
return;
}
}
}
ArrayList<ContentProviderOperation> contentProviderArray = new ArrayList<ContentProviderOperation>();
Builder builder = ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI);
builder.withValue(ContactsContract.RawContacts.ACCOUNT_TYPE,null);
builder.withValue(ContactsContract.RawContacts.ACCOUNT_NAME,null);
ContentProviderOperation contentProvider = builder.build();
contentProviderArray.add(contentProvider);
contentProviderArray.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID,0)
.withValue(ContactsContract.Data.MIMETYPE,StructuredName.CONTENT_ITEM_TYPE)
.withValue(StructuredName.DISPLAY_NAME, officeText.getText().toString())
.build());
contentProviderArray.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID,0)
.withValue(ContactsContract.Data.MIMETYPE,ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Phone.TYPE, Phone.TYPE_MOBILE)
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, phoneText.getText().toString())
.build());
contentProviderArray.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(ContactsContract.Data.MIMETYPE,StructuredPostal.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.StructuredPostal.CITY,addressText.getText().toString())
.build());
/*contentProviderArray.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(ContactsContract.Data.MIMETYPE,StructuredPostal.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.StructuredPostal.,addressText.getText().toString())
.build()); */
getContentResolver().applyBatch(ContactsContract.AUTHORITY, contentProviderArray);
Toast.makeText(ContactUsActivity_2_0.this,getResources().getString(R.string.contact_added),Toast.LENGTH_SHORT).show();
}
catch (OperationApplicationException e) {
Toast.makeText(ContactUsActivity_2_0.this,getResources().getString(R.string.contact_failed_to_added),Toast.LENGTH_SHORT).show();
} catch (RemoteException e) {
Toast.makeText(ContactUsActivity_2_0.this,getResources().getString(R.string.contact_failed_to_added),Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}