Android Write Contacts example with ContentProviderOperation
July 04, 2015
This page will walk through the android write contacts example with ContentProviderOperation. In our example we will write contacts with display name and mobile number. ContentProviderOperation has methods to prepare builder for insert, update and delete contacts. And finally using ContentResolver. applyBatch(), we persist data in which we have to pass authority and operation to apply as arguments. To write contacts, we have to add WRITE_CONTACTS permission in AndroidManifest.xml as given below.
<uses-permission android:name="android.permission.WRITE_CONTACTS"/>
ContentProviderOperation
android.content.ContentProviderOperation is used to insert, update and delete contacts. In our example we will insert contacts. Find the some methods of ContentProviderOperation.newDelete(Uri uri): Creates builder to delete the contact.
newInsert(Uri uri): Creates builder to insert the contact.
newUpdate(Uri uri): Creates builder to update the contact.
ContentProviderOperation.Builder
A builder class which is instantiated by above methods ofContentProviderOperation
. Find some methods which are commonly used.
withValue(String key, Object value) : Adds value to builder that will be inserted or updated. key is the name of value and value is value itself.
withValueBackReference(String key, int previousResult): Adds content values back refrence.
To insert a contact we write code as follows.
contentProviderOperations.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI) .withValue(RawContacts.ACCOUNT_TYPE, null).withValue(RawContacts.ACCOUNT_NAME, null).build()); //insert contact display name using Data.CONTENT_URI contentProviderOperations.add(ContentProviderOperation.newInsert(Data.CONTENT_URI) .withValueBackReference(Data.RAW_CONTACT_ID, 0).withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE) .withValue(StructuredName.DISPLAY_NAME, displayName).build()); //insert mobile number using Data.CONTENT_URI contentProviderOperations.add(ContentProviderOperation.newInsert(Data.CONTENT_URI) .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0).withValue(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE) .withValue(Phone.NUMBER, number).withValue(Phone.TYPE, Phone.TYPE_MOBILE).build());
getApplicationContext().getContentResolver(). applyBatch(ContactsContract.AUTHORITY, contentProviderOperations);
Code to Write Contacts
Here we are proving complete code to write contact. Using for loop we will write three contacts with display name and phone number.MainActivity.java
package com.concretepage; import java.util.ArrayList; import android.app.Activity; import android.content.ContentProviderOperation; import android.content.OperationApplicationException; import android.os.Bundle; import android.os.RemoteException; import android.provider.ContactsContract; import android.provider.ContactsContract.CommonDataKinds.Phone; import android.provider.ContactsContract.CommonDataKinds.StructuredName; import android.provider.ContactsContract.Data; import android.provider.ContactsContract.RawContacts; import android.view.Menu; import android.widget.TextView; public class MainActivity extends Activity { private String[] names = {"Brahma", "Vishnu", "Mahesh"}; private String[] numbers = {"7359753876","3865986365","5286746529"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); for(int i=0; i< names.length;i++){ writeContact(names[i], numbers[i]); } TextView view = (TextView)findViewById(R.id.out); view.setText("Contact saved"); } private void writeContact(String displayName, String number) { ArrayListcontentProviderOperations = new ArrayList (); //insert raw contact using RawContacts.CONTENT_URI contentProviderOperations.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI) .withValue(RawContacts.ACCOUNT_TYPE, null).withValue(RawContacts.ACCOUNT_NAME, null).build()); //insert contact display name using Data.CONTENT_URI contentProviderOperations.add(ContentProviderOperation.newInsert(Data.CONTENT_URI) .withValueBackReference(Data.RAW_CONTACT_ID, 0).withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE) .withValue(StructuredName.DISPLAY_NAME, displayName).build()); //insert mobile number using Data.CONTENT_URI contentProviderOperations.add(ContentProviderOperation.newInsert(Data.CONTENT_URI) .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0).withValue(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE) .withValue(Phone.NUMBER, number).withValue(Phone.TYPE, Phone.TYPE_MOBILE).build()); try { getApplicationContext().getContentResolver(). applyBatch(ContactsContract.AUTHORITY, contentProviderOperations); } catch (RemoteException e) { e.printStackTrace(); } catch (OperationApplicationException e) { e.printStackTrace(); } } @Override protected void onDestroy() { super.onDestroy(); } @Override public boolean onCreateOptionsMenu(Menu menu) { return true; } }
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.concretepage" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="11"/> <uses-permission android:name="android.permission.WRITE_CONTACTS"/> <application android:allowBackup ="false" android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name=".MainActivity" android:theme="@android:style/Theme.NoTitleBar"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>