使用Eclipse 为Nokia手机开发移动应用程序(1)
作者: 来源:it167 点击: 日期:2007-01-28 |
|
|
TutorialMidlet类
TutorialMidlet类是为应用程序提供输入执行的类。Java运行时环境(Java Runtime Environment,JRE)首先会实例化这个类,然后调用其startApp()方法启动midlet。在用户终止应用程序时,可以调用destroyApp()方法。
TutorialMidlet类控制并显示应用程序中的所有UI屏幕。所有用户生成的软键(soft-key)事件(比如用户按下一个软键时)都由TutorialMidlet类处理,因为它实现了CommandListener接口,并将自己作为所有屏幕对象的命令监听程序附加到该接口上。UI事件回调方法是commandAction()。
package tutorial;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
public class TutorialMidlet extends MIDlet
implements CommandListener {
Display display;
Command greetCommand;
Command exitCommand;
Command clearCommand;
Command backCommand;
WelcomeScreen welcomeScreen;
HelloScreen helloScreen;
// instantiate the internal variables
public TutorialMidlet () {
display = Display.getDisplay(this);
greetCommand =
new Command ("Greet", Command.OK, 0);
exitCommand =
new Command ("Exit", Command.EXIT, 0);
clearCommand =
new Command ("Clear", Command.CANCEL, 1);
backCommand =
new Command ("Back", Command.SCREEN, 1);
welcomeScreen = new WelcomeScreen ();
welcomeScreen.addCommand (greetCommand);
welcomeScreen.addCommand (clearCommand);
welcomeScreen.setCommandListener (this);
helloScreen = new HelloScreen ();
helloScreen.addCommand (exitCommand);
helloScreen.addCommand (backCommand);
helloScreen.setCommandListener (this);
}
// Called when the MIDlet is started by the AMS
protected void startApp () {
display.setCurrent (welcomeScreen);
}
protected void pauseApp () {
// Do nothing
}
protected void destroyApp (boolean unconditional) {
notifyDestroyed ();
}
public void commandAction (Command c, Displayable d) {
if (c == greetCommand) {
String name = welcomeScreen.getName ();
helloScreen.setName(name);
display.setCurrent (helloScreen);
} else if (c == clearCommand) {
welcomeScreen.setName("");
display.setCurrent(welcomeScreen);
} else if (c == backCommand) {
display.setCurrent (welcomeScreen);
} else if (c == exitCommand) {
destroyApp (true);
}
}
}
|
实时错误检验
TutorialMidlet类使用WelcomeScreen和HelloScreen类,这些类您也必须键入。Eclipse中的高级Java编辑器会用红色线条(red bar)标出相关的代码路径,警告您存在这类冲突。如果您将鼠标放在红线上,编辑器会显示一个解释框,告诉您为什么它认为这是一个错误。实时语法检验允许开发人员利用Java编译器的一致性检验功能,而不用实际等待编译的完成。

图15:实时错误检验
如果选定Eclipse Project菜单中的Build automatically选项,那么每当更新项目时,Eclipse都会试着不断在后台构建该项目。在这种情况下,Package Explorer也会显示在构建过程中检测到的编译错误。

图16:自动构建错误
|