How to Add New Contacts in Android

How to add contacts programmatically in Android?

Try to use following code

ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
int rawContactInsertIndex = ops.size();

Log.i("Line38", "Here");
ops.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI)
.withValue(RawContacts.ACCOUNT_TYPE, AccountManager.KEY_ACCOUNT_TYPE)
.withValue(RawContacts.ACCOUNT_NAME, AccountManager.KEY_ACCOUNT_NAME)
.build());

ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
.withValueBackReference(Data.RAW_CONTACT_ID, rawContactInsertIndex)
.withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE)
.withValue(StructuredName.DISPLAY_NAME, "u232786seee")
.withValue(StructuredName.IN_VISIBLE_GROUP,true)
.build());

ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex)
.withValue(ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER,"23232343434")
.withValue(ContactsContract.CommonDataKinds.Phone.TYPE, "4343")
.build());

ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex)
.withValue(ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Email.DATA, "")
.withValue(ContactsContract.CommonDataKinds.Email.TYPE, "")
.build());

//Log.i("Line43", Data.CONTENT_URI.toString()+" - "+rawContactInsertIndex);

try {
getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (OperationApplicationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

And add below permission in manifest file

<uses-permission android:name="android.permission.WRITE_CONTACTS"/>

Add new contacts (36 items)

Your problem is with your back references when adding the additional contact data. The back reference should refer to the position of your RawContacts.CONTENT_URI insert within your list of operations (ops), not the position of the contact within your raw data file. You can fix this by keeping track of the size of ops through each iteration:

if (!contactExists(mActivity, split[2])) {
int backRef = ops.size();

ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
.withValue(...)
.build());

ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, backRef)
.withValue(...)
.build()

.build());
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, backRef)
.withValue(...)
.build());

try {
mActivity.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
} catch (Exception e) {
e.printStackTrace();
}
}

How can I add a phone number to an existing contact via Intent?

this is the way to do it:

Intent i = new Intent(Intent.ACTION_INSERT_OR_EDIT);
i.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
i.putExtra(Insert.NAME, "TESTTEST");
i.putExtra(Insert.PHONE, "209384");
startActivity(i);

Add new contact via intent with multiple phone numbers

Found a solution. It consists in using ContentValues:

    Intent intent = new Intent(Intent.ACTION_INSERT);
intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
ArrayList<ContentValues> data = new ArrayList<ContentValues>();

// Filling data with phone numbers
for (int i = 0; i < numberOfPhones; i++) {
ContentValues row = new ContentValues();
row.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
row.put(Phone.NUMBER, PhonesNumberList.get(i));
// Setting the type of this phone number to be of Phone.TYPE_WORK
row.put(Phone.TYPE, Phone.TYPE_WORK);
data.add(row);
}

intent.putExtra(ContactsContract.Intents.Insert.NAME, mName);
intent.putParcelableArrayListExtra(ContactsContract.Intents.Insert.DATA, data);
startActivity(contactIntent);

how to add an existing contact to the Phone contacts Account

After searching I have found that the best way to insert a Contact to the Local Phone contacts is to assign the ACCOUNT_TYPE, ACCOUNT_NAME to null take a look at this Link



Related Topics



Leave a reply



Submit