×
Namespaces

Variants
Actions
(Difference between revisions)

Adding a contact to Phonebook in Java ME

From Nokia Developer Wiki
Jump to: navigation, search
vltsoy (Talk | contribs)
(New page: __NOTOC__ __NOEDITSECTION__ {{CodeSnippet |id=  |platform=S60 3rd Edition, FP2 |devices=Nokia E70, Nokia N78 |category=Java ME |subcategory=Phonebook/Contacts DB |creationdate=October...)
 
hamishwillee (Talk | contribs)
m (Hamishwillee - Fix error in ArticleMetaData. Add Abstract.)
(15 intermediate revisions by 6 users not shown)
Line 1: Line 1:
__NOTOC__
+
[[Category:Java ME]][[Category:PIM]][[Category:File Connection and PIM API (JSR-75)]][[Category:Code Snippet]][[Category:Series 40]][[Category:Symbian]][[Category:Series 40 3rd Edition FP1]][[Category:S60 3rd Edition (initial release)]][[Category:S60 3rd Edition FP1]][[Category:S60 3rd Edition FP2]][[Category:Series 40 6th Edition FP1]][[Category:Series 40 Developer Platform 2.0]][[Category:Nokia Belle]][[Category:Code Examples]]
__NOEDITSECTION__
+
{{ArticleMetaData <!-- v1.2 -->
{{CodeSnippet
+
|sourcecode= [[Media:AddingContacts_src.zip]]
|id=&nbsp;
+
|installfile= [[Media:AddingContacts_bin.zip]]
|platform=S60 3rd Edition, FP2
+
|devices= Nokia 6131, Nokia E70, Nokia N78, Nokia C3-01, Nokia Asha 306, Nokia E7-00
|devices=Nokia E70, Nokia N78
+
|sdk= [http://www.developer.nokia.com/Develop/Java/ Nokia SDK 1.1 for Java], [http://www.developer.nokia.com/Develop/Java/ Nokia SDK 2.0 for Java(beta)], [http://www.developer.nokia.com/info/sw.nokia.com/id/ec866fab-4b76-49f6-b5a5-af0631419e9c/S60_All_in_One_SDKs.html/ Nokia Symbian SDKs]
|category=Java ME
+
|platform= Series 40, S60, Nokia Belle
|subcategory=Phonebook/Contacts DB
+
|devicecompatability= <!-- Compatible devices (e.g.: All* (must have GPS) ) -->
|creationdate=October 20, 2008
+
|dependencies= <!-- Any other/external dependencies e.g.: Google Maps Api v1.0 -->
|keywords=javax.microedition.pim.PIM, javax.microedition.pim.PIMItem, javax.microedition.pim.ContactList.createContact
+
|signing= <!-- Empty or one of Self-Signed, DevCert, Manufacturer -->
 +
|capabilities= <!-- Capabilities required by the article/code example (e.g. Location, NetworkServices. -->
 +
|keywords= javax.microedition.pim.PIM, javax.microedition.pim.PIMItem, javax.microedition.pim.ContactList.createContact
 +
|language= <!-- Language category code for non-English topics - e.g. Lang-Chinese -->
 +
|translated-by= <!-- [[User:XXXX]] -->
 +
|translated-from-title= <!-- Title only -->
 +
|translated-from-id= <!-- Id of translated revision -->
 +
|review-by= <!-- After re-review: [[User:username]] -->
 +
|review-timestamp= <!-- After re-review: YYYYMMDD -->
 +
|update-by= <!-- After significant update: [[User:username]]-->
 +
|update-timestamp= <!-- After significant update: YYYYMMDD -->
 +
|creationdate= 20081127
 +
|author= [[User:Vltsoy]]
 +
<!-- The following are not in current metadata -->
 +
|id= CS001183
 
}}
 
}}
 
  
 
==Overview==
 
==Overview==
  
This snippet demonstrates how to add specific contact into phone contact list.  
+
{{Abstract|This code snippet demonstrates how to add a specific contact to the contact list of the device using Java ME.}}
  
Application creates few input fields. After user fills them and press <tt>«Save»</tt> button, contact with entered data will be added to contact list, using method <tt>ContactList.createContact()</tt>.  
+
The application creates a few input fields. After the user fills them in and presses the  {{Icode|«Save»}} button, contact information with entered data will be added to the contact list, using the method {{Icode|ContactList.createContact()}}.  
  
If any errors occur, message with error description will be diplayed.
+
If an error occurs, a message describing the error is displayed.
  
  
Line 41: Line 54:
 
import javax.microedition.pim.PIMException;
 
import javax.microedition.pim.PIMException;
 
import javax.microedition.pim.PIMItem;
 
import javax.microedition.pim.PIMItem;
import javax.microedition.pim.UnsupportedFieldException;
 
  
 
public class AddContact extends MIDlet implements CommandListener {
 
public class AddContact extends MIDlet implements CommandListener {
Line 50: Line 62:
 
     private Command exitCommand;
 
     private Command exitCommand;
 
     private Command addCommand;
 
     private Command addCommand;
   
 
 
     // Text fields for contact data.
 
     // Text fields for contact data.
 
     private TextField familyName;
 
     private TextField familyName;
Line 58: Line 69:
  
 
     /**
 
     /**
     * Constructor. Constructs the object and initializes displayables.
+
     * Constructor. Constructs the object and initialises displayables.
 
     */
 
     */
 
     public AddContact() {
 
     public AddContact() {
Line 71: Line 82:
 
         givenName = new TextField("Given name", "", 20, TextField.ANY);
 
         givenName = new TextField("Given name", "", 20, TextField.ANY);
 
         familyName = new TextField("Family name", "", 20, TextField.ANY);
 
         familyName = new TextField("Family name", "", 20, TextField.ANY);
         number = new TextField("Telephone", "", 20, TextField.PHONENUMBER);
+
         number = new TextField("Telephone", "", 20, TextField.NUMERIC);
 
         birthday = new DateField("Birthdate", DateField.DATE,
 
         birthday = new DateField("Birthdate", DateField.DATE,
 
                 TimeZone.getTimeZone("GMT"));
 
                 TimeZone.getTimeZone("GMT"));
Line 143: Line 154:
  
 
             Contact contact = contacts.createContact();
 
             Contact contact = contacts.createContact();
            try {
 
                // Add values to new contact from input fields
 
                String[] name =
 
                        new String[contacts.stringArraySize(Contact.NAME)];
 
  
                if (contacts.isSupportedArrayElement(
+
            String[] name =
                        Contact.NAME, Contact.NAME_FAMILY)) {
+
                    new String[contacts.stringArraySize(Contact.NAME)];
                    name[Contact.NAME_FAMILY] = familyName.getString();
+
 
                }
+
            if (contacts.isSupportedArrayElement(
                if (contacts.isSupportedArrayElement(
+
                    Contact.NAME, Contact.NAME_FAMILY)) {
                        Contact.NAME, Contact.NAME_GIVEN)) {
+
                name[Contact.NAME_FAMILY] = familyName.getString();
                    name[Contact.NAME_GIVEN] = givenName.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);
 
                 contact.addStringArray(Contact.NAME, PIMItem.ATTR_NONE, name);
 +
            }
  
 +
            if (contacts.isSupportedField(Contact.TEL)) {
 
                 contact.addString(Contact.TEL, Contact.ATTR_HOME,
 
                 contact.addString(Contact.TEL, Contact.ATTR_HOME,
 
                         number.getString());
 
                         number.getString());
 +
            }
 +
            if (contacts.isSupportedField(Contact.BIRTHDAY)) {
 
                 contact.addDate(Contact.BIRTHDAY, PIMItem.ATTR_NONE,
 
                 contact.addDate(Contact.BIRTHDAY, PIMItem.ATTR_NONE,
 
                         birthday.getDate().getTime());
 
                         birthday.getDate().getTime());
            } catch (UnsupportedFieldException e) {
 
                showError("Unsupported field.", form);
 
                return;
 
 
             }
 
             }
 
             contact.commit();
 
             contact.commit();
Line 176: Line 188:
 
     * Shows error message
 
     * Shows error message
 
     * @param message the message that will be displayed
 
     * @param message the message that will be displayed
     * @param displayable the displayable where the message will be showen
+
     * @param displayable the displayable where the message will be shown
 
     */
 
     */
 
     private void showError(String message, Displayable displayable) {
 
     private void showError(String message, Displayable displayable) {
Line 191: Line 203:
 
==Postconditions==
 
==Postconditions==
  
This code adds new contact into phone contact list.
+
A new contact has been added to the contact list.
  
 
==Supplementary material==
 
==Supplementary material==
  
You can see source file and executable application in attached zip archive. Archive is available for download at [[Media:Adding_a_contact_to_phonebook_in_J2ME.zip‎]]
+
The source file and an executable application are available at [[Media:Adding a contact to phonebook in J2ME.zip‎]].
 
+
[[Category:Java ME]][[Category:Code Examples]]
+

Revision as of 07:25, 2 October 2012

Article Metadata
Code Example
Installation file: Media:AddingContacts_bin.zip
Tested with
Devices(s): Nokia 6131, Nokia E70, Nokia N78, Nokia C3-01, Nokia Asha 306, Nokia E7-00
CompatibilityArticle
Keywords: javax.microedition.pim.PIM, javax.microedition.pim.PIMItem, javax.microedition.pim.ContactList.createContact
Created: vltsoy (27 Nov 2008)
Last edited: hamishwillee (02 Oct 2012)

Contents

Overview

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: AddContact.java

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);
form.addCommand(addCommand);
form.addCommand(exitCommand);
// 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,
TimeZone.getTimeZone("GMT"));
 
birthday.setDate(new Date());
 
form.append(givenName);
form.append(familyName);
form.append(number);
form.append(birthday);
 
form.setCommandListener(this);
 
display = Display.getDisplay(this);
display.setCurrent(form);
}
 
/**
* 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) {
notifyDestroyed();
} else if (cmd == addCommand) {
saveContact();
}
}
 
/**
* Adds contact to PIM contact list.
*/

private void saveContact() {
ContactList contacts = null;
try {
// Retrieve the contact list
contacts = (ContactList) PIM.getInstance().openPIMList(
PIM.CONTACT_LIST, PIM.READ_WRITE);
 
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,
number.getString());
}
if (contacts.isSupportedField(Contact.BIRTHDAY)) {
contact.addDate(Contact.BIRTHDAY, PIMItem.ATTR_NONE,
birthday.getDate().getTime());
}
contact.commit();
contacts.close();
} 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("");
alert.setTitle("Error");
alert.setString(message);
alert.setType(AlertType.ERROR);
alert.setTimeout(5000);
display.setCurrent(alert, displayable);
}
}

Postconditions

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 J2ME.zip‎.

288 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×