Created
February 24, 2016 01:30
-
-
Save luanvuhlu/eda581a0131527a43e01 to your computer and use it in GitHub Desktop.
Add a new contact on Android
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void addContact(ContactDetail c) { | |
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>(); | |
int rawContactInsertIndex = ops.size(); | |
ops.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI) | |
.withValue(RawContacts.ACCOUNT_TYPE, null) | |
.withValue(RawContacts.ACCOUNT_NAME, null).build()); | |
//Phone Number | |
if(null!=c.getPhoneNumber()){ | |
ops.add(ContentProviderOperation | |
.newInsert(ContactsContract.Data.CONTENT_URI) | |
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, | |
rawContactInsertIndex) | |
.withValue(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE) | |
.withValue(Phone.NUMBER, c.getPhoneNumber()) | |
.withValue(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE) | |
.withValue(Phone.TYPE, "1").build()); | |
} | |
//Display name/Contact name | |
ops.add(ContentProviderOperation | |
.newInsert(ContactsContract.Data.CONTENT_URI) | |
.withValueBackReference(Data.RAW_CONTACT_ID, | |
rawContactInsertIndex) | |
.withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE) | |
.withValue(StructuredName.DISPLAY_NAME, c.getName()) | |
.build()); | |
//Email details | |
if(c.getEmail()!=null){ | |
ops.add(ContentProviderOperation | |
.newInsert(ContactsContract.Data.CONTENT_URI) | |
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, | |
rawContactInsertIndex) | |
.withValue(Data.MIMETYPE, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE) | |
.withValue(ContactsContract.CommonDataKinds.Email.DATA, c.getEmail()) | |
.withValue(Data.MIMETYPE, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE) | |
.withValue(ContactsContract.CommonDataKinds.Email.TYPE, "2").build()); | |
} | |
//Postal Address | |
ops.add(ContentProviderOperation | |
.newInsert(ContactsContract.Data.CONTENT_URI) | |
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, | |
rawContactInsertIndex) | |
.withValue(Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE ) | |
.withValue(ContactsContract.CommonDataKinds.StructuredPostal.POBOX, "Postbox") | |
.withValue(Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE ) | |
.withValue(ContactsContract.CommonDataKinds.StructuredPostal.STREET, "street") | |
.withValue(Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE ) | |
.withValue(ContactsContract.CommonDataKinds.StructuredPostal.CITY, "city") | |
.withValue(Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE ) | |
.withValue(ContactsContract.CommonDataKinds.StructuredPostal.REGION, "region") | |
.withValue(Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE ) | |
.withValue(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE, "postcode") | |
.withValue(Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE ) | |
.withValue(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY, "country") | |
.withValue(Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE ) | |
.withValue(ContactsContract.CommonDataKinds.StructuredPostal.TYPE, "3") | |
.build()); | |
//Organization details | |
ops.add(ContentProviderOperation | |
.newInsert(ContactsContract.Data.CONTENT_URI) | |
.withValueBackReference(Data.RAW_CONTACT_ID, | |
rawContactInsertIndex) | |
.withValue(Data.MIMETYPE, ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE ) | |
.withValue(ContactsContract.CommonDataKinds.Organization.COMPANY, "Devindia") | |
.withValue(Data.MIMETYPE, ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE ) | |
.withValue(ContactsContract.CommonDataKinds.Organization.TITLE, "Developer") | |
.withValue(Data.MIMETYPE, ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE ) | |
.withValue(ContactsContract.CommonDataKinds.Organization.TYPE, "0") | |
.build()); | |
//IM details | |
ops.add(ContentProviderOperation | |
.newInsert(ContactsContract.Data.CONTENT_URI) | |
.withValueBackReference(Data.RAW_CONTACT_ID, | |
rawContactInsertIndex) | |
.withValue(Data.MIMETYPE, ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE) | |
.withValue(ContactsContract.CommonDataKinds.Im.DATA, "ImName") | |
.withValue(Data.MIMETYPE, ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE ) | |
.withValue(ContactsContract.CommonDataKinds.Im.DATA5, "2") | |
.build()); | |
try { | |
ContentProviderResult[] res = 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(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment