我目前正在开发一个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();
}
}
}
答案 0 :(得分:4)
研究jsr75 tag wiki 中建议的规范和教程。
这个问题看起来与您的问题最匹配: Getting Started With the PIM APIs
...本文提供了PIM API的代码密集型入门教程;它:
- 介绍JSR 75
- 描述
javax.microedition.pim
可选包- 提供有关PIM API的详细信息
- 介绍使用它们所涉及的努力
- 提供可以适应您自己的无线应用程序需求的代码
...
上面的教程还列出了一些推荐用于进一步研究的资源更多信息。