How To Add Contacts Programmatically In Android?
I have tried this but Contacts were not added! ContentResolver cr = this.getContentResolver(); ContentValues cv = new ContentValues(); cv.put(ContactsContract.CommonDataKinds.Phone
Solution 1:
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-permissionandroid:name="android.permission.WRITE_CONTACTS"/>
Solution 2:
other option....
publicvoidinsert() {
Intent intent = new Intent(
ContactsContract.Intents.SHOW_OR_CREATE_CONTACT,
ContactsContract.Contacts.CONTENT_URI);
intent.setData(Uri.parse("tel:911"));//specify your number here
intent.putExtra(ContactsContract.Intents.Insert.COMPANY, "Emergency USA");
startActivity(intent);
Toast.makeText(this, "Record inserted", Toast.LENGTH_SHORT).show();
}
Post a Comment for "How To Add Contacts Programmatically In Android?"