Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert contact in Android with ContactsContract

I am trying to add a new contact to the Android 2.2 contacts directly.

//this code doesn't work    
ContentValues cv=new ContentValues();
cv.put(ContactsContract.Contacts.DISPLAY_NAME, "TESTEST");
Uri u= getContentResolver().insert(ContactsContract.Contacts.CONTENT_URI, cv);

gives me the error "Aggregate contacts are created automatically." What am I doing wrong?

This calls the Android's add contact form:

//this code works but it's not ideal
Intent i = new Intent(Intent.ACTION_INSERT_OR_EDIT);
i.setType(Contacts.CONTENT_ITEM_TYPE);
i.putExtra(Insert.NAME, "TESTTEST");
i.putExtra(Insert.PHONE, "209384");
startActivity(i);

I can't figure out how to send it a first name and a last name - only a 'name' which it puts in the first name box. Also I'd like to separate the postal code into street, city, state, and zip which right now is all being put into the street box.

like image 631
Juliet Avatar asked Dec 16 '10 09:12

Juliet


People also ask

How do I edit contacts in Android Studio?

To edit a contact, call Intent(action) to create an intent with the action ACTION_EDIT . Call setDataAndType() to set the data value for the intent to the contact's Contacts. CONTENT_LOOKUP_URI and the MIME type to Contacts.


1 Answers

Add Contact Details in android

import java.util.ArrayList;

import android.app.Activity;
import android.content.ContentProviderOperation;
import android.content.ContentProviderResult;
import android.content.OperationApplicationException;
import android.net.Uri;
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.Contacts.Data;
import android.provider.ContactsContract.RawContacts;

public class DemoAddAddressBook extends Activity {
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  /*
   * Uri newPerson = addContactName();
   *
   * addMobilePhoneNo(newPerson); addEmail(newPerson);
   * addPostalAddress(newPerson); addOrganization(newPerson);
   */

  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
  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, "9X-XXXXXXXXX")
    .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, "Mike Sullivan")
    .build());
  //Email details
  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, "[email protected]")
    .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();
  }

 }
}
like image 116
Rupesh Kumar Avatar answered Sep 18 '22 19:09

Rupesh Kumar