Java Tutorial/J2ME/PIM
PIM Contact
import javax.microedition.lcdui.rumand;
import javax.microedition.lcdui.rumandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;
import javax.microedition.midlet.MIDlet;
import javax.microedition.pim.Contact;
import javax.microedition.pim.ContactList;
import javax.microedition.pim.PIM;
public class SeedMIDlet extends MIDlet implements CommandListener {
private Form mForm = new Form("Data Seeded");
private Command mExitCommand;
public SeedMIDlet() {
try {
PIM pimInst = PIM.getInstance();
contList = (ContactList) pimInst.openPIMList(PIM.CONTACT_LIST,
PIM.READ_WRITE);
Contact ct = contList.createContact();
String[] name = new String[contList.stringArraySize(Contact.NAME)];
name[Contact.NAME_GIVEN] = "firstName";
name[Contact.NAME_FAMILY] = "lastName";
ct.addStringArray(Contact.NAME, Contact.ATTR_NONE, name);
String[] addr = new String[contList.stringArraySize(Contact.ADDR)];
addr[Contact.ADDR_STREET] = "street";
addr[Contact.ADDR_LOCALITY] = "city";
addr[Contact.ADDR_COUNTRY] = "country";
addr[Contact.ADDR_POSTALCODE] = "street";
ct.addStringArray(Contact.ADDR, Contact.ATTR_NONE, addr);
ct.rumit();
if (contList != null)
contList.close();
contList = null;
} catch (Exception ex) {
return;
}
mForm.append(new StringItem(null, "PIM data stored."));
mExitCommand = new Command("Exit", Command.EXIT, 0);
mForm.addCommand(mExitCommand);
mForm.setCommandListener(this);
}
public void startApp() {
Display.getDisplay(this).setCurrent(mForm);
}
public void pauseApp() {
}
public void destroyApp(boolean flg) {
}
public void commandAction(Command c, Displayable s) {
if (c == mExitCommand) {
destroyApp(true);
notifyDestroyed();
}
}
private ContactList contList = null;
}
PIM Demo
import java.util.Enumeration;
import javax.microedition.lcdui.rumand;
import javax.microedition.lcdui.rumandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.List;
import javax.microedition.midlet.MIDlet;
import javax.microedition.pim.Contact;
import javax.microedition.pim.ContactList;
import javax.microedition.pim.PIM;
public class PIMMIDlet extends MIDlet implements CommandListener {
private ContactList contList = null;
private Enumeration contacts = null;
private List mNameList;
private Command mExitCommand= new Command("Exit", Command.EXIT, 0);
public PIMMIDlet() {
try {
PIM pimInst = PIM.getInstance();
contList = (ContactList) pimInst.openPIMList(PIM.CONTACT_LIST, PIM.READ_ONLY);
contacts = contList.items();
} catch (Exception ex) {
return;
}
if (contacts == null)
return;
mNameList = new List("List of contacts", List.EXCLUSIVE);
while (contacts.hasMoreElements()) {
Contact tCont = (Contact) contacts.nextElement();
String[] nameValues = tCont.getStringArray(Contact.NAME, 0);
String firstName = nameValues[Contact.NAME_GIVEN];
String lastName = nameValues[Contact.NAME_FAMILY];
mNameList.append(lastName + ", " + firstName, null);
}
mNameList.addCommand(mExitCommand);
mNameList.setCommandListener(this);
}
public void startApp() {
Display.getDisplay(this).setCurrent(mNameList);
}
public void pauseApp() {
}
public void destroyApp(boolean flg) {
}
public void commandAction(Command c, Displayable s) {
if (c == mExitCommand) {
destroyApp(true);
notifyDestroyed();
}
}
}
PIM Install Tester
import java.io.IOException;
public class PIMInstallTester {
public static void main(String[] a) throws Exception {
String version = null;
version = System.getProperty("microedition.pim.version");
if (version != null) {
if (!version.equals("1.0"))
throw new IOException("Package is not version 1.0.");
} else
throw new IOException("PIM optional package is not available.");
}
}