Sunday, August 2, 2009

Contacts in Android

Working with Contacts (PIM) in Android doesn't seem to be straightforward as in Java ME. Looks like Android has a taken a 'database like' approach.

For e.g., there is no way to delete all the contacts in the phone! To do this we wrote a simple application which does the following:


mContentResolver = this.getContentResolver();



int contactsDeleted = mContentResolver.delete(Contacts.People.CONTENT_URI, null, null);

To delete a contact with a particular field (say name), use the following statement:


mContentResolver.delete(Contacts.People.CONTENT_URI,
Contacts.People.NAME + "=?",
new String[] {"Sriram V Iyer"} );

The first argument is the URI (which happens to be People in this case), the second argument is like a database WHERE query (without the WHERE keyword) and the third argument is the values (string array) that fills the content of the query.

To remove a contact with a particular phone number, use this as second argument:
Contacts.People.NUMBER + "?="