×
Namespaces

Variants
Actions
Revision as of 15:09, 12 October 2008 by extkbeditor1 (Talk | contribs)

Listing PIM databases using JSR-75

From Nokia Developer Wiki
Jump to: navigation, search


Template:KBCS

Overview

The Personal Information Manager (PIM) API for Java ME (JSR-75) allows mobile Java applications to read/write to/from the locally stored personal information databases. There are three categories of PIM databases: contacts, calendar, and to-do. This code snippet demonstrates how to list PIM databases (also called PIM lists) that exist on the device.

Note: Don't be surprised if the MIDlet asks several times for permission to read user data. This is due to the javax.microedition.pim.PIM.listPIMLists() method call and normal behavior for untrusted MIDlets.

Source file: PIMMIDlet.java

import javax.microedition.lcdui.Display;
import javax.microedition.midlet.MIDlet;
public class PIMMIDlet extends MIDlet {
private PIMDbForm pimDatabases;
 
/**
* Constructor. Constructs the object and initializes displayables.
*/

public PIMMIDlet() {
pimDatabases = new PIMDbForm(this);
}
 
/**
* From MIDlet.
* Called when the MIDlet is started.
*/

public void startApp() {
Display.getDisplay(this).setCurrent(pimDatabases);
}
 
/**
* 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
}
}

Source file: PIMDbForm.java

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;
import javax.microedition.pim.PIM;
/**
* A list of PIM databases.
*/

public class PIMDbForm extends Form implements CommandListener {
private Command exitCommand;
private PIMMIDlet parent;
private int itemCounter = 0; // How many items are listed
 
public PIMDbForm(PIMMIDlet parent) {
super("PIM databases");
 
this.parent = parent;
 
exitCommand = new Command("Exit", Command.EXIT, 0);
addCommand(exitCommand);
setCommandListener(this);
 
// Retrieve the PIM instance
PIM pim = PIM.getInstance();
// Add each type of PIM lists (PIM databases) to the form
addList(PIM.CONTACT_LIST, pim);
addList(PIM.EVENT_LIST, pim);
addList(PIM.TODO_LIST, pim);
}
 
public void commandAction(Command command, Displayable displayable) {
if (command == exitCommand) {
// Exit the MIDlet
parent.notifyDestroyed();
}
}
 
/**
* Adds the names of specific types of PIM lists (databases) to the form.
* @param listType the type of list
* @param pim PIM instance from which the databases are queried
*/

private void addList(int listType, PIM pim) {
String[] list = pim.listPIMLists(listType);
for (int i = 0; i < list.length; i++) {
itemCounter++;
StringItem item = new StringItem("Database " + itemCounter, list[i]);
append(item);
}
}
}

Postconditions

The MIDlet displays all PIM databases that exist on the device on a form.

128 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.

×