You can use the following approach:
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
ops.add(ContentProviderOperation
.newInsert(ContactsContract.RawContacts.CONTENT_URI)
.withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)
.withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null)
.build());
ops.add(ContentProviderOperation
.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(
ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
.withValue(
ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME,
edtNome.getEditText().getText().toString()).build());
ops.add(ContentProviderOperation
.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(
ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
.withValue(
ContactsContract.CommonDataKinds.Email.ADDRESS,
edtEmail.getEditText().getText().toString()).build());
ops.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.NUMBER,
edtTelefone.getEditText().getText().toString())
.withValue(ContactsContract.CommonDataKinds.Phone.TYPE,
ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE).build());
try {
ContentProviderResult[] contentProviderResult = contentResolver.applyBatch(ContactsContract.AUTHORITY, ops);
if (contentProviderResult[0].uri == null) {
throw new Exception("Erro ao incluir usuário");
} else {
new SnackBar.Builder(this)
.withMessage("Usuário incluído aos contatos com sucesso!")
.withStyle(SnackBar.Style.DEFAULT)
.withDuration((short) 3000)
.show();
}
} catch (Exception e) {
e.printStackTrace();
}
You have to pass Contact_id as a parameter to know that it is an edit, if it is an inclusion, it will go with 0.
if (contentProviderResult[0].Uri == null) { what I did is not required, in my case I treated it to see if there was success in the insertion.
– Carlos Bridi
The example above are some tips, email, you can study this excerpt: ops.add(Contentprovideroperation . newInsert(ContactsContract.Data.CONTENT_URI)
 . withValueBackReference(Contactscontract.Data.RAW_CONTACT_ID, 0) . withValue( Contactscontract.Data.MIMETYPE, Contactscontract.CommonDataKinds.Email.CONTENT_ITEM_TYPE) . withValue( Contactscontract.CommonDataKinds.Email.ADDRESS, edtEmail.getEditText(). gettext(). toString()). build());
– Carlos Bridi
Carlos, thank you. I will study yes and as soon as I do, I mark as an answer or I put my doubts.
– Henrique