我目前正在开发 J2ME 应用程序事件管理器。我正在使用 PIM,但我对此没有足够的了解。有人可以帮助我或给我有关如何设置显示,编辑和删除记录的参考吗?
这是我到目前为止所做的。
import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.TextField;
import javax.microedition.lcdui.DateField;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Alert;
import javax.microedition.pim.PIM;
import javax.microedition.pim.PIMItem;
import javax.microedition.pim.EventList;
import javax.microedition.pim.Event;
import javax.microedition.pim.PIMException;
import java.util.Date;
public class AddCalendarEvent extends MIDlet implements CommandListener {
private Display display;
private Form addEventForm;
private Command cmdAddEvent;
private Command cmdExit;
private TextField summaryField;
private DateField startDateField;
private DateField endDateField;
private TextField noteField;
private TextField locationField;
public AddCalendarEvent() {
if(checkPIMSupport() == false) {
exitMIDlet();
}
initializeComponents();
}
public void initializeComponents() {
display = Display.getDisplay(this);
addEventForm = new Form("Add event");
cmdAddEvent = new Command("Add event", Command.SCREEN, 0);
addEventForm.addCommand(cmdAddEvent);
cmdExit = new Command("Exit", Command.EXIT, 0);
addEventForm.addCommand(cmdExit);
addEventForm.setCommandListener(this);
try {
EventList eventList = (EventList)PIM.getInstance().openPIMList(
PIM.EVENT_LIST, PIM.READ_WRITE);
if(eventList.isSupportedField(Event.SUMMARY) == true) {
summaryField = new TextField("Summary", null, 20,
TextField.ANY);
addEventForm.append(summaryField);
} else {
eventList.close();
throw new Exception("Summary field is not supported");
}
if(eventList.isSupportedField(Event.START) == true) {
startDateField = new DateField("Start date",
DateField.DATE_TIME);
startDateField.setDate(new Date());
addEventForm.append(startDateField);
}
if(eventList.isSupportedField(Event.END) == true) {
endDateField = new DateField("End date", DateField.DATE_TIME);
endDateField.setDate(new Date());
addEventForm.append(endDateField);
}
if(eventList.isSupportedField(Event.NOTE) == true) {
noteField = new TextField("Note", null, 20, TextField.ANY);
addEventForm.append(noteField);
}
if(eventList.isSupportedField(Event.LOCATION) == true) {
locationField = new TextField("Location", null, 20,
TextField.ANY);
addEventForm.append(locationField);
}
eventList.close();
} catch(PIMException pimExc) {
}
catch(SecurityException secExc) {
}
catch(Exception exc) {
exitMIDlet();
}
}
private boolean checkPIMSupport() {
String propValue = System.getProperty("microedition.pim.version");
if(propValue != null) {
return true;
} else {
return false;
}
}
private void addEvent() {
try {
EventList eventList = (EventList)PIM.getInstance().openPIMList(
PIM.EVENT_LIST, PIM.READ_WRITE);
Event event = eventList.createEvent();
if(eventList.isSupportedField(Event.SUMMARY) == true) {
String summary = summaryField.getString();
event.addString(Event.SUMMARY, PIMItem.ATTR_NONE, summary);
} else {
eventList.close();
throw new Exception("Summary field for event is not supported");
}
if(eventList.isSupportedField(Event.START) == true) {
long startDate = startDateField.getDate().getTime();
event.addDate(Event.START, PIMItem.ATTR_NONE, startDate);
}
if(eventList.isSupportedField(Event.END) == true) {
long endDate = endDateField.getDate().getTime();
event.addDate(Event.END, PIMItem.ATTR_NONE, endDate);
}
if(eventList.isSupportedField(Event.NOTE) == true) {
String note = noteField.getString();
event.addString(Event.NOTE, PIMItem.ATTR_NONE, note);
}
if(eventList.isSupportedField(Event.LOCATION) == true) {
String location = locationField.getString();
event.addString(Event.LOCATION, PIMItem.ATTR_NONE, location);
}
event.commit();
eventList.close();
showAlert("Info", "Event was successfully added.");
} catch(PIMException pimExc) {
showAlert("PIMException", pimExc.getMessage());
}
catch(SecurityException secExc) {
showAlert("SecurityException", secExc.getMessage());
}
catch(Exception exc) {
showAlert("Exception", exc.getMessage());
}
}
private void showAlert(String title, String message) {
Alert alert = new Alert(title);
alert.setString(message);
alert.setTimeout(Alert.FOREVER);
display.setCurrent(alert);
}
public void startApp() {
display.setCurrent(addEventForm);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
private void exitMIDlet() {
notifyDestroyed();
}
public void commandAction(Command command, Displayable displayable) {
if(command == cmdAddEvent) {
addEvent();
}
if(command == cmdExit) {
exitMIDlet();
}
}
}