Logo Icon Logo
A Crowd-sourced Cookbook on Writing Great Android® Apps
GitHub logo Twitter logo OReilly Book Cover Art

Adding a Contact through the Contacts Content provider

Author: Ian Darwin
Published? true -- FormatLanguage: W

Problem:

You have some contact information that you want to save for use in the Contacts application.

Solution:

Set up a list of operations for batch insert, and tell the persistence manager to run it.

Discussion:

The Contacts database is, to be sure, "flexible". It has to adapt to many different kinds of accounts and contact management uses, with different types of data. And it is, as a result, somewhat complicated.

Note: In current versions, the classes named Contacs (and by extension all its inner classes and interfaces) are deprecated, meaning "don't use in new development". The classes and interfaces that take their place have names being with (the somewhat cumbersome, and somewhat tongue-twisty) ContactsContract.

We'll start with the simplest case of adding a person's contact information. We want to insert this information - which we either got from the user or by finding it on the network someplace:

Name: Jon Smith
Home Phone 416-555-5555
Work Phone 416-555-6666
Email jon@jonsmith.domain

First we have to determine which Account to associate the data with (see Accounts). For now we will use a fake Account name ("darwinian" is both an adjective, and my name, so we'll use that).

For each of the four fields, we'll need to create an Account Operation.

We add all five operations to a List, and pass that into getContentResolver().applyBatch().

Here is the code for the addContact() method.

	private void addContact() {
		final String ACCOUNT_NAME = "darwinian"
		String name = "Jon Smith";
		String homePhone = "416-555-5555";
		String workPhone = "416-555-6666";
		String email = "jon@jonsmith.domain";

		// Use new-style batch operations: Build List of ops then call applyBatch
		try {
			ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
			AuthenticatorDescription[] types = accountManager.getAuthenticatorTypes();
			ops.add(ContentProviderOperation.newInsert(
					ContactsContract.RawContacts.CONTENT_URI).withValue(
							ContactsContract.RawContacts.ACCOUNT_TYPE, types[0].type)
							.withValue(ContactsContract.RawContacts.ACCOUNT_NAME, ACCOUNT_NAME)
							.build());
			ops.add(ContentProviderOperation
					.newInsert(ContactsContract.Data.CONTENT_URI)
					.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
					.withValue(ContactsContract.Data.MIMETYPE,
							ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
							.withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME,name)
							.build());
			ops.add(ContentProviderOperation.newInsert(
					ContactsContract.Data.CONTENT_URI).withValueBackReference(
							ContactsContract.Data.RAW_CONTACT_ID, 0).withValue(
									ContactsContract.Data.MIMETYPE,
									ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
									.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER,
											homePhone).withValue(
													ContactsContract.CommonDataKinds.Phone.TYPE,
													ContactsContract.CommonDataKinds.Phone.TYPE_HOME)
													.build());
			ops.add(ContentProviderOperation.newInsert(
					ContactsContract.Data.CONTENT_URI).withValueBackReference(
							ContactsContract.Data.RAW_CONTACT_ID, 0).withValue(
									ContactsContract.Data.MIMETYPE,
									ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
									.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER,
											workPhone).withValue(
													ContactsContract.CommonDataKinds.Phone.TYPE,
													ContactsContract.CommonDataKinds.Phone.TYPE_WORK)
													.build());
			ops.add(ContentProviderOperation.newInsert(
					ContactsContract.Data.CONTENT_URI).withValueBackReference(
							ContactsContract.Data.RAW_CONTACT_ID, 0).withValue(
									ContactsContract.Data.MIMETYPE,
									ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
									.withValue(ContactsContract.CommonDataKinds.Email.DATA, email)
									.withValue(ContactsContract.CommonDataKinds.Email.TYPE,
											ContactsContract.CommonDataKinds.Email.TYPE_HOME)
											.build());

			getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
			
			Toast.makeText(this, getString(R.string.addContactSuccess),
					Toast.LENGTH_LONG).show();
		} catch (Exception e) {
			
			Toast.makeText(this, getString(R.string.addContactFailure),
					Toast.LENGTH_LONG).show();
			Log.e(LOG_TAG, getString(R.string.addContactFailure), e);
		}
	}

The resulting contact shows up in the Contact Manager or People app. If it is.not initially visible, go to the main Contacts list page, pressMenu, select Display Options, and select groups until it does appear. Alternately, you can Search in All Contacts and it will show up.

No records found.