I’ve been dabbling lately with Google’s Android platform for mobile development. While there aren’t any announced devices for the marketplace yet, it’s an extremely open platform, and one of the simplest mobile platforms to develop for that I’ve encountered.
The Android SDK still isn’t “officially” released yet, which means there are lots of undocumented quirks in the system and a few inconsistencies. Some of them you can figure out through trial and error. Some are just annoying. I recently managed to triumph over one of these quirks and thought I would share the fruits of that labor. In this case, it’s specifically how to add a contact, phone and email address into the built-in Address Book using Google’s Contacts ContentProvider.
Let’s start by adding a new person to the address book. Just a name and nothing else.
// create a new name
ContentValues values = new ContentValues();
values.put(Contacts.People.NAME, "Test Name");
// add it to the database
ContentURI newPerson = getContentResolver().insert(Contacts.People.CONTENT_URI, values);
The
where the number 1 indicates the new ID of the person you just created.
newPerson
ContentURI that’s returned by the insert()
call is null if something went wrong. If it’s not null, then it might look something like thiscontent://contacts/people/1
where the number 1 indicates the new ID of the person you just created.
If you don’t get back an ID number, then you can’t proceed, but assuming all went well, then you can use that new ID number to add other information to the existing record. For example, to add a phone number:
// assign the new phone number to the person
values.clear();
values.put(Contacts.Phones.PERSON_ID, newPerson.getPathLeaf());
values.put(Contacts.Phones.NUMBER, "(800) 466-4411");
// insert the new phone number in the database
getContentResolver().insert(Contacts.Phones.CONTENT_URI, values);
and to add an email address:
// assign an email address for this person
values.clear();
values.put(Contacts.ContactMethods.PERSON_ID, newPerson.getPathLeaf());
values.put(Contacts.ContactMethods.KIND, Contacts.ContactMethods.EMAIL_KIND);
values.put(Contacts.ContactMethods.TYPE,Contacts.ContactMethods.EMAIL_KIND_HOME_TYPE);
values.put(Contacts.ContactMethods.DATA, "test@test.com");
// insert the new email address in the database
getContentResolver().insert(newPerson.addPath(Contacts.ContactMethods.CONTENT_URI.getPath()), values);
Adding an email is tricky because it’s doesn’t match the same coding convention as adding a new contact or adding a new phone number. To make matters worse, this mismatch/inconsistency is not documented well in the existing docs, so it required a lot of trial and error to figure out how to do this without causing an internal error in the emulator.
Essentially, the issue is that for contacts and phone numbers you can use a ContentURI like this:
to retrieve existing records or to add new records to the system.
content://contacts/people
content://contacts/phones
to retrieve existing records or to add new records to the system.
However, for emails this is not the case. For emails, you have to use different URIs for each function, specifically:
retrieve emails –
insert new emails –
where the number 1 is the ID of the person you want to add the emails for.
retrieve emails –
content://contacts/contact_methods
insert new emails –
content://contacts/people/1/contact_methods
where the number 1 is the ID of the person you want to add the emails for.
I’m sure this will become documented more clearly as Android starts reaching a wider developer community. Until then, hopefully these code snippets will save some time for those Android developers struggling to get their apps finished before the March 2nd deadline for Google’s $25,000 Android competition.
Have fun coding.
No hay comentarios:
Publicar un comentario