Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries. Thanks for all your past and future contributions.

Revision as of 05:49, 26 July 2013 by hamishwillee (Talk | contribs)

Adding a contact to Phonebook in Java ME

From Wiki
Jump to: navigation, search
Article Metadata
Code Example
Installation file: Media:AddingContacts
Tested with
Devices(s): Nokia Asha 310, Nokia Asha 501, Nokia 6131, Nokia E70, Nokia N78, Nokia C3-01, Nokia Asha 306, Nokia E7-00
Created: vltsoy (27 Nov 2008)
Reviewed: skalogir (09 May 2013)
Last edited: hamishwillee (26 Jul 2013)



This code snippet demonstrates how to add a specific contact to the contact list of the device using Java ME.

The application creates a few input fields. After the user fills them in and presses the «Save» button, contact information with entered data will be added to the contact list, using the method ContactList.createContact().

If an error occurs, a message describing the error is displayed.

Source file:

import java.util.Date;
import java.util.TimeZone;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.TextField;
import javax.microedition.lcdui.DateField;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.midlet.MIDlet;
import javax.microedition.pim.Contact;
import javax.microedition.pim.ContactList;
import javax.microedition.pim.PIM;
import javax.microedition.pim.PIMException;
import javax.microedition.pim.PIMItem;
public class AddContact extends MIDlet implements CommandListener {
//Main form.
private Form form;
private Display display;
private Command exitCommand;
private Command addCommand;
// Text fields for contact data.
private TextField familyName;
private TextField givenName;
private TextField number;
private DateField birthday;
* Constructor. Constructs the object and initialises displayables.

public AddContact() {
form = new Form("Add PIM Contact");
// Add command buttons
addCommand = new Command("Save", Command.OK, 0);
exitCommand = new Command("Exit", Command.EXIT, 1);
// Create input fields
givenName = new TextField("Given name", "", 20, TextField.ANY);
familyName = new TextField("Family name", "", 20, TextField.ANY);
number = new TextField("Telephone", "", 20, TextField.NUMERIC);
birthday = new DateField("Birthdate", DateField.DATE,
birthday.setDate(new Date());
display = Display.getDisplay(this);
* From MIDlet.
* Called when the MIDlet is started.

public void startApp() {
// Check for availability of PIM interface.
if (System.getProperty("microedition.pim.version") == null) {
showError("PIM API not supported.", form);
* From MIDlet.
* Called to signal the MIDlet to enter the Paused state.

public void pauseApp() {
// No implementation required.
* From MIDlet.
* Called to signal the MIDlet to terminate.
* @param unconditional whether the MIDlet has to be unconditionally
* terminated

public void destroyApp(boolean unconditional) {
// No implementation required
* From CommandListener.
* Called by the system to indicate that a command has been invoked on a
* particular displayable.
* @param cmd the command that was invoked
* @param displayable the displayable where the command was invoked

public void commandAction(Command cmd, Displayable displayable) {
if (cmd == exitCommand) {
} else if (cmd == addCommand) {
* Adds contact to PIM contact list.

private void saveContact() {
ContactList contacts = null;
try {
// Retrieve the contact list
contacts = (ContactList) PIM.getInstance().openPIMList(
Contact contact = contacts.createContact();
String[] name =
new String[contacts.stringArraySize(Contact.NAME)];
if (contacts.isSupportedArrayElement(
Contact.NAME, Contact.NAME_FAMILY)) {
name[Contact.NAME_FAMILY] = familyName.getString();
if (contacts.isSupportedArrayElement(
Contact.NAME, Contact.NAME_GIVEN)) {
name[Contact.NAME_GIVEN] = givenName.getString();
if (contacts.isSupportedField(Contact.NAME)) {
contact.addStringArray(Contact.NAME, PIMItem.ATTR_NONE, name);
if (contacts.isSupportedField(Contact.TEL)) {
contact.addString(Contact.TEL, Contact.ATTR_HOME,
if (contacts.isSupportedField(Contact.BIRTHDAY)) {
contact.addDate(Contact.BIRTHDAY, PIMItem.ATTR_NONE,
} catch (PIMException e) {
showError(e.getMessage(), form);
* Shows error message
* @param message the message that will be displayed
* @param displayable the displayable where the message will be shown

private void showError(String message, Displayable displayable) {
Alert alert = new Alert("");
display.setCurrent(alert, displayable);


A new contact has been added to the contact list.

Supplementary material

The source file and an executable application are available at Media:Adding a contact to phonebook in‎.

158 page views in the last 30 days.