You are on page 1of 6

Creating and removing contact lists You can create contact lists on a BlackBerry device by invoking BlackBerryPIM.createPIMList().

Currently, the PIM list type you specify when you invoke createPIMList() must be PIM.CONTACT_LIST or a PIMException is thrown. You can also remove contact lists by invoking BlackBerryPIM.removePIMList(). Currently, this method supports PIM lists only of type PIM.CONTACT_LIST. Create a contact list You can create contact lists on a BlackBerry device. Note the following about contact lists that you create:

name[Contact.NAME_GIVEN] = "Noha"; name[Contact.NAME_FAMILY] = "Toma"; contact.addStringArray(Contact.NAME, PIMItem.ATTR_NONE, name); contact.addString(Contact.TEL, Contact.ATTR_HOME, "519-5550151"); contact.addString(Contact.TEL, Contact.ATTR_WORK, "519-5550199"); contact.commit(); // close the contact list contactList.close();


1. 2. 3. 4. 5. 6. 7. 8.

Each contact list has a unique ID, which is the value that is returned by createPIMList(). The contact lists do not have service records and do not support wireless synchronization. Applications can register to listen for changes to your contact list by invoking BlackBerryPIMList.addListener(). Import the required classes and interfaces. import javax.microedition.pim.Contact; import javax.microedition.pim.PIM; import javax.microedition.pim.PIMException; import javax.microedition.pim.PIMItem; import net.rim.blackberry.api.pdap.BlackBerryContact; import net.rim.blackberry.api.pdap.BlackBerryContactList; import net.rim.blackberry.api.pdap.BlackBerryPIM; import net.rim.blackberry.api.pdap.BlackBerryPIMList;

} catch (PIMException e) { System.out.println(e.getMessage()); } Remove a contact list You can remove contact lists from a BlackBerry device. Note the following about removing contact lists:

Retrieve a BlackBerryPIM object. BlackBerryPIM myPIM = (BlackBerryPIM) PIM.getInstance(); 10. Create the contact list. long listUID = myPIM.createPIMList(PIM.CONTACT_LIST, "test"); The contact list is named using the name that you provide ("test" in the preceding example), unless there is another contact list with that name on the device, in which case a number is appended to the name to make it unique. To reference the contact list later, you should use the list's UID, which is the value that is returned by createPIMList(). 11. Optionally, populate the contact list. 12. BlackBerryContactList contactList = 13. (BlackBerryContactList) myPIM.openPIMList(PIM.CONTACT_LIST, 14. PIM.READ_WRITE, listUID); 15. Contact contact = contactList.createContact(); 16. String[] name = new String[contactList.stringArraySize(Contact.NAME)]; 17. name[Contact.NAME_GIVEN] = "Noha"; 18. name[Contact.NAME_FAMILY] = "Toma"; 19. contact.addStringArray(Contact.NAME, PIMItem.ATTR_NONE, name); contact.commit(); 20. Close the contact list. contactList.close(); Code sample BlackBerryPIM myPIM = (BlackBerryPIM) PIM.getInstance(); try { // create a contact list long listUID = myPIM.createPIMList(PIM.CONTACT_LIST, "test"); // add a contact to the list BlackBerryContactList contactList = (BlackBerryContactList) myPIM.openPIMList(PIM.CONTACT_LIST, PIM.READ_WRITE, listUID); Contact contact = contactList.createContact(); String[] name = new String[contactList.stringArraySize(Contact.NAME)];

9.

You cannot remove the default contact list (which has the UID -1). A BlackBerryPIMRemovalException is thrown if an error occurs when you try to remove a contact list. 1. Import the required classes and interfaces. 2. import javax.microedition.pim.PIM; 3. import net.rim.blackberry.api.pdap.BlackBerryPIM; import net.rim.blackberry.api.pdap.BlackBerryPIMRemovalException;

You cannot remove contact lists that have service records. You cannot remove the last remaining contact list on a device.

4.
5. 6.

7.

Retrieve a BlackBerryPIM object. BlackBerryPIM myPIM = (BlackBerryPIM) PIM.getInstance(); If you know the name or UID of the contact list that you want to remove, go to step 6. Retrieve the array of contact lists. String[] lists = myPIM.listPIMLists(PIM.CONTACT_LIST); The returned array provides the names that are assigned to each contact list. Iterate through the array to search for the contact list that you want to remove.

Remove the contact list by invoking BlackBerryPIM.removePIMList(). You can provide either the name or the UID of the contact list. By default, the method removes a contact list only if the list is empty. If you want to remove a contact list that is not empty, you must provide the parameter BlackBerryPIM.REMOVE_NON_EMPTY_LIST. 9. try 10. { 11. myPIM.removePIMList(PIM.CONTACT_LIST, "test", 12. BlackBerryPIM.REMOVE_NON_EMPTY_LIST); 13. } 14. 15. catch (BlackBerryPIMRemovalException e) 16. { 17. // handle the exception } Code sample BlackBerryPIM myPIM = (BlackBerryPIM) PIM.getInstance(); try { myPIM.removePIMList(PIM.CONTACT_LIST, "test", BlackBerryPIM.REMOVE_NON_EMPTY_LIST); } catch (BlackBerryPIMRemovalException e) {

8.

System.out.println(e.getMessage()); } Delete a contact You can delete a contact from the default contact list or another contact list on a BlackBerry device. 1. Import the required classes and interfaces. 2. import net.rim.blackberry.api.pdap.BlackBerryContact; 3. import net.rim.blackberry.api.pdap.BlackBerryPIMList; 4. import net.rim.device.api.system.ControlledAccessException; 5. import javax.microedition.pim.Contact; 6. import javax.microedition.pim.ContactList; 7. import javax.microedition.pim.PIM; import javax.microedition.pim.PIMException;

the specified phone number. The Vector is empty if there are no matching contacts. Vector contacts = Phone.getContactsByPhoneNumber(phoneNum);

b.

c. d.

8.

To delete a contact from the default contact list, invoke PIM.openPIMList(int, int) to open the default contact list instance, passing in as parameters the type of list to open (PIM.CONTACT_LIST), and the access mode with which to open the list (PIM.READ_WRITE, PIM.READ_ONLY, or PIM.WRITE_ONLY). Proceed to step 4. 9. BlackBerryContactList contactList = (BlackBerryContactList) PIM.getInstance().openPIMList(PIM.CONTACT_LIST, PIM.READ_WRITE); 10. To delete a contact from a contact list that is not the default contact list, perform the following actions:

Code sample try { BlackBerryContactList list = (BlackBerryContactList) PIM.getInstance().openPIMList(PIM.CONTACT_LIST, PIM.READ_WRITE, "test");

To search a specified contact list, invoke BlackBerryContactList.itemsByPhoneNumber(), which returns an Enumeration object of all contacts that match the specified phone number. Each of the items in the Enumeration is a PIMItem object, which you can cast to a BlackBerryContactList object. BlackBerryContactList list = (BlackBerryContactList) PIM.getInstance().openPIMList(PIM.CONTACT_LI ST, PIM.READ_WRITE, "test"); Enumeration myEnum = list.itemsByPhoneNumber(phoneNum);

Enumeration myEnum = list.itemsByPhoneNumber(phoneNum); while (myEnum.hasMoreElements()) { Object o = myEnum.nextElement(); if (o instanceof BlackBerryContact) { BlackBerryContact c = (BlackBerryContact) o; String[] name = c.getStringArray(Contact.NAME, 0); add(new RichTextField("A matching contact is " + name[Contact.NAME_GIVEN] + " " + name[Contact.NAME_FAMILY])); } } } catch (Exception e) { System.out.println(e.getMessage()); } Notify an application when a contact list changes You can register your application to receive notification of changes to the contact lists on a BlackBerry device by implementing the PIMListListener interface. The PIMListListener interface provides the following methods:

a.

Invoke listPIMLists(int pimListType) to return an array of String objects. The returned array provides the system-assigned name for each contact list. The default contact list is returned at index 0 of the array. String[] lists = PIM.listPIMLists(PIM.CONTACT_LIST); Iterate over the array that PIM.listPIMLists() returns to search for the system-assigned name for the contact list that you want to delete. Invoke PIM.openPIMList(int, int, String) to open the contact list instance, passing in as parameters the type of list to open (PIM.CONTACT_LIST), the access mode with which to open the list (PIM.READ_WRITE, PIM.READ_ONLY, or PIM.WRITE_ONLY), and the contact list name. BlackBerryContactList contactList = (BlackBerryContactList) PIM.getInstance().openPIMList(PIM.CONTACT_LI ST, PIM.READ_WRITE, name);

b. c.

d.

11. Invoke BlackBerryContactList.removeContact() to delete the


contact from the contact list. contactList.removeContact(contact);

ControlledAccessException if your application does not have permission to access the application that it invokes. Retrieve contacts by phone number You can search all contact lists or a specified contact list on a BlackBerry device for contacts that match a specified phone number. 1. Import the required classes and interfaces. 2. import net.rim.blackberry.api.pdap.BlackBerryContact; 3. import net.rim.blackberry.api.pdap.BlackBerryContactList; 4. import net.rim.blackberry.api.pdap.BlackBerryPIMList; 5. import net.rim.blackberry.api.phone.Phone; 6. import java.util.*; 7. import javax.microedition.pim.PIM; 8. import javax.microedition.pim.PIMException; import javax.microedition.pim.PIMItem; 9. Do one of the following:

12. Check for PIMException, and check for


1. 2. 3. 4. 5. 6. 7.

itemAdded(PIMItem item), which is invoked when an item is added to a contact list itemUpdated(PIMItem oldItem, PIMItem newItem), which is invoked when an item changes itemRemoved(PIMItem item), which is invoked when an item is deleted from a contact list Import the required classes and interfaces. import net.rim.blackberry.api.pdap.BlackBerrryContact; import net.rim.blackberry.api.pdap.BlackBerryContactList; import net.rim.blackberry.api.pdap.BlackBerryPIMList; import net.rim.blackberry.api.pdap.PIMListListener; import net.rim.device.api.system.ControlledAccessException; import javax.microedition.pim.PIMList; import javax.microedition.pim.PIMException; To receive change notifications for the default contact list, invoke PIM.openPIMList(int, int) to open the default contact list instance, passing in as parameters the type of list to open (PIM.CONTACT_LIST), and the access mode with which to open the list (PIM.READ_WRITE, PIM.READ_ONLY, or PIM.WRITE_ONLY). Proceed to step 4.

8.

a.

To search all contacts lists, invoke Phone.getContactsByPhoneNumber(), which returns a Vector object that contains the contacts that match

9.

BlackBerryContactList contactList = (BlackBerryContactList) PIM.getInstance().openPIMList(PIM.CONTACT_LIST, PIM.READ_WRITE); 10. To receive change notifications for a contact list that is not the default contact list, perform the following actions:

a.

Invoke listPIMLists(int), passing in as the parameter the type of list to open (PIM.CONTACT_LIST), to return an array of String objects. The returned array provides the system-assigned name for each contact list. The default contact list is returned at index 0 of the array. String[] lists = PIM.listPIMLists(PIM.CONTACT_LIST); Iterate over the array that PIM.listPIMLists() returns to search for the system-assigned name for the contact list that you want to receive change notifications for. Invoke PIM.openPIMList(int, int, String) to open the contact list instance, passing in as parameters the type of list to open (PIM.CONTACT_LIST), the access mode with which to open the list (PIM.READ_WRITE, PIM.READ_ONLY, or PIM.WRITE_ONLY), and the contact list name. BlackBerryContactList contactList = (BlackBerryContactList) PIM.getInstance().openPIMList(PIM.CONTACT_LI ST, PIM.READ_WRITE, name);

b.

c.

d.

11. Invoke BlackBerryPIMList.addListener() to register a listener


for the contact list. (BlackBerryPIMList) contactList.addListener(new PIMListListener());

12. Override the PIMListListener.itemAdded(),

13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29.

PIMListListener.itemUpdated(), and PIMListListener.itemRemoved() methods to configure the notification behavior. public void itemAdded(PIMItem item) { System.out.println("ITEM ADDED: " + (BlackBerryContact) item.getString(BlackBerryContact.UID, 0)); } public void itemUpdated(PIMItem oldItem, PIMItem newItem) { System.out.println("ITEM UPDATED: " + (BlackBerryContact) oldItem.getString(BlackBerryContact.UID, 0) + " to " + (BlackBerryContact) newItem.getString(Contact.UID, 0)); }

public void itemRemoved(PIMItem item) { System.out.println("ITEM REMOVED: " + (BlackBerryContact) item.getString(BlackBerryContact.UID, 0)); 30. } ControlledAccessException if your application does not have permission to access the application that it invokes. Open the contacts application You can open the contacts application on the BlackBerry device by using the Invoke.invokeApplication() method, and passing in the relevant arguments. 1. Import the required classes and interfaces. import net.rim.blackberry.api.invoke.Invoke; import net.rim.blackberry.api.invoke.AddressBookArguments; import net.rim.device.api.system.ControlledAccessException;

31. Check for PIMException, and check for

2. Invoke Invoke.invokeApplication(APP_TYPE_ADDRESSBOOK, AddressBookArguments). AddressBookArguments abArg = new AddressBookArguments(); Invoke.invokeApplication(Invoke.APP_TYPE_ADDRESSBOOK, abArg); 3. Check for a ControlledAccessException if your application does not have permission to access the application that it invokes. Open the contacts application by using contact data You can open the contacts application on a BlackBerry device and display a contact by using the Invoke.invokeApplication() method, and passing in contact data as a parameter of an AddressBookArguments object. 1. Import the required classes and interfaces. import net.rim.blackberry.api.invoke.AddressBookArguments; import net.rim.blackberry.api.invoke.Invoke; import net.rim.blackberry.api.pdap.BlackBerryContact; import net.rim.blackberry.api.pdap.BlackBerryContactList; import net.rim.device.api.system.ControlledAccessException; import javax.microedition.pim.PIM; import javax.microedition.pim.PIMException; 2. Invoke PIM.getInstance() to retrieve a PIM instance, and invoke PIM.openPIMList(int, int) to open the default contact list, passing in as parameters the type of list to open (PIM.CONTACT_LIST) and the access mode with which to open the list (PIM.READ_WRITE, PIM.READ_ONLY, or IM.WRITE_ONLY). To open a named contact list, you can instead invoke PIM.openPIMList(int, int, String). BlackBerryContactList contactList = (BlackBerryContactList) PIM.getInstance().openPIMList(PIM.CONTACT_LIST, PIM.READ_WRITE); 3. Invoke BlackBerryContactList.getByUID(String uid) to retrieve a contact from the contact list. BlackBerryContact contact = contactList.getByUID("1XKIOD898"); 4. Create an instance of the AddressBookArguments class, passing in a Contact object as a parameter. AddressBookArguments abArg = new AddressBookArguments("ARG_VIEW", contact); 5. Invoke Invoke.invokeApplication(APP_TYPE_ADDRESSBOOK, AddressBookArguments) and use the AddressBookArguments object for the contact. Invoke.invokeApplication(Invoke.APP_TYPE_ADDRESSBOOK, abArg); 6. Check for PIMException, and check for ControlledAccessException if your application does not have permission to access the application that it invokes. Open the contacts application with a specific contact list You can open the contacts application on a BlackBerry device and display a specific contact list by invoking the BlackBerryContactList.choose() method. 1. Import the required classes and interfaces. import net.rim.blackberry.api.pdap.BlackBerryContact; import net.rim.blackberry.api.pdap.BlackBerryContactGroup; import net.rim.blackberry.api.pdap.BlackBerryContactList; import net.rim.blackberry.api.pdap.BlackBerryPIM; import net.rim.blackberry.api.pdap.BlackBerryPIMList; import net.rim.device.api.system.ControlledAccessException; import javax.microedition.pim.PIM; import javax.microedition.pim.PIMException; import javax.microedition.pim.PIMItem; 2. Invoke PIM.listPIMLists(int pimListType) to return an array of String objects. The returned array provides the system-assigned names, one for each PIM list of the specified type. The default list of the specified type is returned at index 0 of the array. String[] lists = PIM.listPIMLists(PIM.CONTACT_LIST); 3. Iterate over the array that is returned from PIM.listPIMLists() to search for the system-assigned name for the contact list that you want to display.

4. Invoke BlackBerryPIMList.getPIMListUID() to return the UID for contact list. long uid = cl.getPIMListUID(); 5. Invoke PIM.getInstance() to retrieve a PIM instance, and invoke PIM.openPIMList(int, int, long) to open the contact list, passing in as parameters the type of list to open (PIM.CONTACT_LIST), the access mode with which to open the list (PIM.READ_WRITE, PIM.READ_ONLY, or PIM.WRITE_ONLY), and the UID. BlackBerryContactList list = (BlackBerryContactList) PIM.getInstance().openPIMList(PIM.CONTACT_LIST, PIM.READ_WRITE, uid); 6. Invoke BlackBerryContactList.choose() to return a BlackBerryContact or a BlackBerryContactGroup PIMItem. PIMItem item = list.choose(); if (item instanceof BlackBerryContact) { BlackBerryContact contact = (BlackBerryContact) item; int values = contact.countValues(BlackBerryContact.EMAIL); String email = contact.getString(BlackBerryContact.EMAIL, 0); System.out.println("Email is: " + email); } else if (item instanceof BlackBerryContactGroup) Development Guide Open the contacts application with a specific contact list { ... } 7. Check for PIMException, and check for ControlledAccessException if your application does not have permission to access the application that it invokes. Create a contact and assign it to a contact list You can create a contact and assign it to either the default contact list or another contact list on a BlackBerry device. 1. Import the required classes and interfaces. import net.rim.blackberry.api.pdap.BlackBerryContact; import net.rim.blackberry.api.pdap.BlackBerryContactList; import net.rim.blackberry.api.pdap.BlackBerryPIMList; import net.rim.device.api.system.ControlledAccessException; import javax.microedition.pim.PIM; import javax.microedition.pim.PIMList; import javax.microedition.pim.PIMException; import javax.microedition.pim.ContactList; 2. To add the new contact to the default contact list, invoke PIM.openPIMList(int, int) to open the default contact list instance, passing in as parameters the type of list to open (PIM.CONTACT_LIST) and the PIM.READ_WRITE access mode. Proceed to step 4. BlackBerryContactList contactList = (BlackBerryContactList) PIM.getInstance().openPIMList(PIM.CONTACT_LIST, PIM.READ_WRITE); 3. To add the new contact to a contact list that is not the default contact list, perform the following actions: a. Invoke PIM.listPIMLists(int), passing in as the parameter the type of list (PIM.CONTACT_LIST), to return an array of String objects. The returned array provides the system-assigned names for each contact list. The default contact list is returned at index 0 of the array. String[] lists = PIM.listPIMLists(PIM.CONTACT_LIST); b. Iterate over the array that PIM.listPIMLists() returns to search for the system-assigned name for the contact list that you want to open. c. Invoke PIM.openPIMList(int, int, String) to open the contact list instance, passing in as parameters the type of list to open (PIM.CONTACT_LIST), the PIM.READ_WRITE access mode, and the contact list name.

BlackBerryContactList contactList = (BlackBerryContactList) PIM.getInstance().openPIMList(PIM.CONTACT_LIST, PIM.READ_WRITE, name); 4. Invoke ContactList.createContact() to add the new contact to the contact list. BlackBerryContact contact = contactList.createContact(); 5. Invoke one or more of the following methods to add information for the new contact. For more information about PIMItem methods, see the API reference for the BlackBerry Java Development Environment. addString() addStringArray() addDate() addInt() addBoolean() addBinary() 6. Invoke the following methods to verify that the information meets the size requirements and type requirements for the specified contact field. Invoke ContactList.isSupportedField(int) to verify that the item supports the field type. Invoke ContactList.isSupportedAttribute(int, int) to verify that the field supports the specified attribute. Invoke PIMList.maxValues(int field) to verify the number of values that the field supports. 7. Invoke Contact.commit() to commit the changes. if(contact.isModified()) { contact.commit(); } 8. Check for PIMException, and check for ControlledAccessException if your application does not have permission to access the application that it invokes. Retrieve contact information You can retrieve information from a contact list on a BlackBerry device by invoking one of the PIMList.items() methods. These methods return an enumeration of all the contacts in a specific contact list. You can invoke the BlackBerryContactList.items() methods to return contact groups. 1. Import the required classes and interfaces. import net.rim.blackberry.api.pdap.BlackBerryContact; import net.rim.blackberry.api.pdap.BlackBerryContactList; import net.rim.blackberry.api.pdap.BlackBerryPIMList; import java.util.Enumeration; import javax.microedition.pim.PIM; import javax.microedition.pim.PIMException; import javax.microedition.pim.PIMItem; 2. Invoke PIM.getInstance() to retrieve a PIM instance, and invoke PIM.openPIMList() to open a contact list instance, passing in as parameters the type of list to open (PIM.CONTACT_LIST), the access mode with which to open the list (PIM.READ_WRITE, PIM.READ_ONLY, or PIM.WRITE_ONLY), and the name if you are not opening the default contact list. BlackBerryContactList contactList = (BlackBerryContactList) PIM.getInstance().openPIMList(PIM.CONTACT_LIST, PIM.READ_WRITE); 3. Invoke PIMList.items() to retrieve an enumeration of items in a contact list. Enumeration _enum = contactList.items(); 4. Invoke one of the PIMItem getter methods to retrieve contact information. To retrieve an array of fields that contain the data for a specified contact, invoke PIMItem.getFields(). To retrieve a String that represents the value for a specified contact field, invoke PIMItem.getString(int field, int index). To retrieve a date that represents the value for a specified contact field, invoke PIMItem.getDate(int field, int index). while (_enum.hasMoreElements())

{ BlackBerryContact c = (BlackBerryContact)_enum.nextElement(); int[] fieldIds = c.getFields(); int id; for(int index = 0; index < fieldIds.length; ++index) { id = fieldIds[index]; if(c.getPIMList().getFieldDataType(id) == BlackBerryContact.STRING) { for(int j=0; j < c.countValues(id); ++j) { String value = c.getString(id, j); System.out.println(c.getPIMList(). getFieldLabel(id) + "="+ value); } } } } Retrieve a contact list UID You can open a contact list on a BlackBerry device by specifying a system-assigned name or a UID. The UID is associated with a BlackBerry Enterprise Server account and persists across all BlackBerry device changes, including OS updates. You can open the contact list by its UID by invoking BlackBerryPIM.openPIMList(int pimListType, int mode, long uid). 1. Import the required classes and interfaces. import net.rim.blackberry.api.pdap.BlackBerryContactList; import net.rim.blackberry.api.pdap.BlackBerryPIMList; import javax.microedition.pim.PIM; import javax.microedition.pim.PIMException; import javax.microedition.pim.PIMItem; 2. Access the contact list that you want to work with. 3. Invoke BlackBerryPIMList.getPIMListUID() to retrieve the UID of the contact list. long uid = list.getPIMListUID(); Export a contact You can export contact information from a contact list on a BlackBerry device to an output stream. The export process converts a PIM item to a stream of bytes that another application can import. You can export PIM data to a supported serial format by invoking PIM.toSerialFormat(PIMItem, OutputStream, String, String), and passing in as arguments the PIMItem, the OutputStream to which the serialized PIMItem is written, the character encoding format to use when writing to the output stream, and the supported serial format to convert to, such as vCard. 1. Import the required classes and interfaces. import java.io.UnsupportedEncodingException; import java.util.Enumeration; import javax.microedition.pim.Contact; import javax.microedition.pim.ContactList; import javax.microedition.pim.PIM; import javax.microedition.pim.PIMException; 2. Invoke PIM.supportedSerialFormats() and specify the list type (PIM.CONTACT_LIST) to retrieve a string array of the supported serial formats. ContactList contactList = (ContactList) PIM.getInstance().openPIMList(PIM.CONTACT_LIST, PIM.READ_WRITE); String[] dataFormats = PIM.getInstance().supportedSerialFormats(PIM.CONTACT_LIST); 3. Invoke PIM.getInstance().toSerialFormat(item, stream, enc, dataFormat)to write an item to a supported serial format. Use the enc parameter to specify the character encoding format to use when writing to the output stream. Supported character encoding formats include UTF8, ISO-8859-1, and UTF-16BE. If the enc parameter is null, the method uses UTF-8.

ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); Enumeration e = contactList.items(); while (e.hasMoreElements()) { try { Contact c = (Contact)e.nextElement(); PIM.getInstance().toSerialFormat(c, byteStream, "UTF8", dataFormats[0]); } catch (UnsupportedEncodingException ex) { System.out.println(ex.toString()); } } Import a contact You can import contact information from a compatible input stream to a contact list on a BlackBerry device. You can import contact information by invoking fromSerialFormat(InputStream, String), and passing in as arguments the InputStream from which the PIMItem is written and the character encoding format to use. Supported character encoding formats include UTF8, ISO-8859-1, and UTF-16BE. 1. Import the required classes and interfaces. import java.io.ByteArrayOutputStream; import javax.microedition.pim.Contact; import javax.microedition.pim.ContactList; import javax.microedition.pim.PIM; import javax.microedition.pim.PIMItem; 2. Invoke PIM.getInstance().fromSerialFormat() to return an array of PIM items. ByteArrayInputStream istream = new ByteArrayInputStream(outputStream.toByteArray()); PIMItem[] pi = PIM.getInstance().fromSerialFormat(istream, "UTF8"); 3. Open a contact list and invoke ContactList.importContact() to create a new contact by using a PIM item. ContactList contactList = (ContactList) PIM.getInstance().openPIMList(PIM.CONTACT_LIST, PIM.READ_WRITE); Contact contact2 = contactList.importContact((Contact) pi[0]); contact2.commit(); Delete a contact You can delete a contact from the default contact list or another contact list on a BlackBerry device. 1. Import the required classes and interfaces. import net.rim.blackberry.api.pdap.BlackBerryContact; import net.rim.blackberry.api.pdap.BlackBerryPIMList; import net.rim.device.api.system.ControlledAccessException; import javax.microedition.pim.Contact; import javax.microedition.pim.ContactList; import javax.microedition.pim.PIM; import javax.microedition.pim.PIMException; 2. To delete a contact from the default contact list, invoke PIM.openPIMList(int, int) to open the default contact list instance, passing in as parameters the type of list to open (PIM.CONTACT_LIST), and the access mode with which to open the list (PIM.READ_WRITE, PIM.READ_ONLY, or PIM.WRITE_ONLY). Proceed to step 4. BlackBerryContactList contactList = (BlackBerryContactList) PIM.getInstance().openPIMList(PIM.CONTACT_LIST, PIM.READ_WRITE); 3. To delete a contact from a contact list that is not the default contact list, perform the following actions: a. Invoke listPIMLists(int pimListType) to return an array of String objects. The returned array provides the system-assigned name for each contact list. The default contact list is returned at index 0 of the array.

String[] lists = PIM.listPIMLists(PIM.CONTACT_LIST); b. Iterate over the array that PIM.listPIMLists() returns to search for the system-assigned name for the contact list that you want to delete. c. Invoke PIM.openPIMList(int, int, String) to open the contact list instance, passing in as parameters the type of list to open (PIM.CONTACT_LIST), the access mode with which to open the list (PIM.READ_WRITE, PIM.READ_ONLY, or PIM.WRITE_ONLY), and the contact list name. BlackBerryContactList contactList = (BlackBerryContactList) PIM.getInstance().openPIMList(PIM.CONTACT_LIST, PIM.READ_WRITE, name); 4. Invoke BlackBerryContactList.removeContact() to delete the contact from the contact list. contactList.removeContact(contact); 5. Check for PIMException, and check for ControlledAccessException if your application does not have permission to access the application that it invokes.

You might also like