📄 alarmmidlet.java
字号:
import java.util.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.util.Timer;
import java.util.TimerTask;
public class AlarmMIDlet extends MIDlet
implements ItemStateListener, CommandListener{
private Command alarmCommand;
private Command resetCommand;
private Command exitCommand;
private Display display;
private DateField alarmTimeSetting;
private Form aForm;
private int dateIndex;
private Date currentTime;
private Timer aTimer;
private alarmTimerTask aTimerTask;
private boolean dateOK = false;
public AlarmMIDlet(){
display = Display.getDisplay(this);
aForm = new Form("设置闹铃时间");
currentTime = new Date();
alarmTimeSetting = new DateField("", DateField.DATE_TIME);
alarmTimeSetting.setDate(currentTime);
alarmCommand = new Command("待命中", Command.SCREEN, 1);
resetCommand = new Command("重置", Command.SCREEN, 1);
exitCommand = new Command("离开", Command.EXIT, 1);
dateIndex = aForm.append(alarmTimeSetting);
aForm.addCommand(alarmCommand);
aForm.addCommand(resetCommand);
aForm.addCommand(exitCommand);
aForm.setCommandListener(this);
aForm.setItemStateListener(this);
}
public void startApp (){
display.setCurrent(aForm);
}
public void pauseApp(){
}
public void destroyApp(boolean unconditional){
}
public void itemStateChanged(Item item){
if (item == alarmTimeSetting){
if (alarmTimeSetting.getDate().getTime() < currentTime.getTime())
dateOK = false;
else
dateOK = true;
}
}
public void commandAction(Command c, Displayable s){
if (c == alarmCommand){
if (dateOK == false){
Alert aAlert = new Alert("无法设置闹铃", "请再做选择", null, null);
aAlert.setTimeout(Alert.FOREVER);
aAlert.setType(AlertType.ERROR);
display.setCurrent(aAlert);
}
else{
aTimer = new Timer();
alarmTimerTask aTimerTask = new alarmTimerTask();
long amount = alarmTimeSetting.getDate().getTime() - currentTime.getTime();
aTimer.schedule(aTimerTask,amount);
aForm.removeCommand(alarmCommand);
aForm.removeCommand(resetCommand);
aForm.delete(dateIndex);
aForm.setTitle("待命中....");
}
}
else if(c == resetCommand){
alarmTimeSetting.setDate(currentTime = new Date());
}
else if(c == exitCommand){
destroyApp(false);
notifyDestroyed();
}
}
class alarmTimerTask extends TimerTask{
public final void run(){
Alert aAlert = new Alert("时间到 !");
aAlert.setTimeout(Alert.FOREVER);
aAlert.setType(AlertType.ALARM);
AlertType.ERROR.playSound(display);
display.setCurrent(aAlert);
cancel();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -