Java Tutorial/J2ME/Timer
Версия от 17:44, 31 мая 2010; (обсуждение)
Alarm with Timer and Alert
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.rumand;
import javax.microedition.lcdui.rumandListener;
import javax.microedition.lcdui.DateField;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.ItemStateListener;
import javax.microedition.midlet.MIDlet;
public class AlarmMIDlet extends MIDlet implements ItemStateListener, CommandListener {
private Command alarmCommand= new Command("alarm", Command.SCREEN, 1);
private Command resetCommand= new Command("Reset", Command.SCREEN, 1);
private Command exitCommand= new Command("Exit", Command.EXIT, 1);
private Display display;
private DateField alarmTimeSetting;
private Form aForm= new Form("Alarm Setting");
private int dateIndex;
private Date currentTime= new Date();
private Timer aTimer;
private boolean dateOK = false;
public AlarmMIDlet() {
display = Display.getDisplay(this);
alarmTimeSetting = new DateField("", DateField.DATE_TIME);
alarmTimeSetting.setDate(currentTime);
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("Cannot set", "Warning", 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("Waiting....");
}
} 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("Alert!");
aAlert.setTimeout(Alert.FOREVER);
aAlert.setType(AlertType.ALARM);
AlertType.ERROR.playSound(display);
display.setCurrent(aAlert);
cancel();
}
}
}
Fixed Rate Schedule MIDlet
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import javax.microedition.lcdui.rumand;
import javax.microedition.lcdui.rumandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.TextBox;
import javax.microedition.midlet.MIDlet;
public class FixedRateScheduleMIDlet extends MIDlet implements CommandListener {
private Command exitCommand = new Command("Exit", Command.EXIT, 1);
private Display display;
TextBox t= new TextBox("Time setting", "", 256, 0);
private Timer aTimer= new Timer();
private Date currentTime= new Date();
private Calendar now = Calendar.getInstance();
public FixedRateScheduleMIDlet() {
display = Display.getDisplay(this);
now.setTime(currentTime);
}
public void startApp() {
ClockTimerTask aTimerTask = new ClockTimerTask();
aTimer.schedule(aTimerTask, 1000, 1000);
t.addCommand(exitCommand);
t.setCommandListener(this);
display.setCurrent(t);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void commandAction(Command c, Displayable s) {
if (c == exitCommand) {
destroyApp(false);
notifyDestroyed();
}
}
class ClockTimerTask extends TimerTask {
public final void run() {
t.setString("" + now.get(Calendar.HOUR) + ":" + now.get(Calendar.MINUTE) + ":"
+ now.get(Calendar.SECOND) + ":");
display.setCurrent(t);
}
}
}
Simple Timer
import java.util.Timer;
import java.util.TimerTask;
import javax.microedition.lcdui.rumand;
import javax.microedition.lcdui.rumandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.TextBox;
import javax.microedition.midlet.MIDlet;
public class SimpleTimerMIDlet extends MIDlet implements CommandListener {
private Command exitCommand = new Command("Exit", Command.EXIT, 1);
private Command scheduleCommand = new Command("schedule", Command.SCREEN, 1);
private TextBox aTextBox = new TextBox("Setting", "Content", 20, 0);
public static String aMessage = "";
Display display;
private Timer aTimer;
private SimpleTimerTask aTimerTask;
public SimpleTimerMIDlet() {
display = Display.getDisplay(this);
}
public void startApp() {
aTextBox.addCommand(exitCommand);
aTextBox.addCommand(scheduleCommand);
aTextBox.setCommandListener(this);
display.setCurrent(aTextBox);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void commandAction(Command c, Displayable s) {
if (c == scheduleCommand) {
aTimer = new Timer();
aTimerTask = new SimpleTimerTask();
aTimer.schedule(aTimerTask, 500);
aTextBox.setString(aMessage);
} else if (c == exitCommand) {
destroyApp(false);
notifyDestroyed();
}
}
}
class SimpleTimerTask extends TimerTask {
public final void run() {
System.out.println("Executing ....");
cancel();
}
}