37
4: Using the address book
See “Code sample: Displaying a screen that lets BlackBerry device users add new contacts” on page 45 for more
information.
Export a contact.
1.
To import or export PIM item data, use an output stream writer to export tasks from the
BlackBerry® device to a supported serial format, such as vCard®.
2. To retrieve a string array of supported formats, invoke
PIM.supportedSerialFormats()
and specify the list type
(PIM.Contact_LIST)
.
3. To write an item to a supported serial format, invoke
toSerialFormat()
. The enc
parameter specifies the character encoding to use when writing to the output stream.
Supported character encodings include "UTF8," "ISO-8859-1," and "UTF-16BE." This
parameter cannot be null.
ContactList contactList =
(ContactList)PIM.getInstance().openPIMList(
PIM.CONTACT_LIST, PIM.READ_ONLY);
String[] dataFormats = PIM.getInstance().supportedSerialFormats(
PIM.CONTACT_LIST);
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
Enumeration e = contactList.items();
while (e.hasMoreElements()) {
Contact c = (Contact)e.nextElement();
PIM.getInstance().toSerialFormat(c, byteStream, "UTF8",
dataFormats[0]);
}
Import a contact.
1.
To return an array of PIM items, invoke
fromSerialFormat()
.
2. To create a new contact using the PIM item, invoke
C
ontactList.importContact();
3. To specify the character encoding to use when writing to the output stream, use the enc
parameter.
// Import contact from vCard.
ByteArrayInputStream istream = new
ByteArrayInputStream(outputStream.toByteArray());
PIMItem[] pi = PIM.getInstance().fromSerialFormat(istream,
"UTF8");
ContactList contactList =
(ContactList)PIM.getInstance().openPIMList(PIM.CONTACT_LIST,
PIM.READ_WRITE);
Contact contact2 = contactList.importContact((Contact)pi[0]);
contact2.commit();
Delete a contact.
>
Invoke
removeContact()
on a contact list.
contactList.removeContact(contact);
Close a contacts list.
>
Invoke
close()
.
try {
contactList.close();
} catch(PIMException e) {
Dialog.alert(e.toString());
}
Task
Steps
Summary of Contents for JAVA DEVELOPMENT ENVIRONMENT - - DEVICE APPLICATIONS INTEGRATION - DEVELOPMENT GUIDE
Page 4: ......
Page 7: ......
Page 10: ...10 BlackBerry Device Applications Integration Guide...
Page 12: ...12 BlackBerry Device Applications Integration Guide...
Page 50: ...50 BlackBerry Device Applications Integration Guide...
Page 56: ...56 BlackBerry Device Applications Integration Guide...
Page 65: ......
Page 66: ...2008 Research In Motion Limited Published in Canada...