📄 alarmmidlet.java
字号:
package com.j2medev.ch7.push;
import java.util.Calendar;
import java.util.Date;
import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
public class AlarmMIDlet extends MIDlet implements CommandListener {
private Display display = null;
private Form form = null;
private AlarmModel model = null;
private Date set = null;
private DateField date = new DateField("启动时间",DateField.DATE_TIME);
public static final Command exitCommand = new Command("退出",Command.EXIT, 1);
public static final Command okCommand = new Command("注册",Command.OK,2);
public static final Command stopCommand = new Command("停止",Command.EXIT, 1);
public static final String className = "com.j2medev.ch7.push.AlarmMIDlet";
protected void startApp(){
if(display == null){
display = Display.getDisplay(this);
form = new Form("PUSH注册");
Date now = new Date();
date.setDate(now);
form.append(date);
form.addCommand(okCommand);
form.addCommand(exitCommand);
form.setCommandListener(this);
model = new AlarmModel();
String snow = model.readDate();
if(snow != null){//RMS中存在以前注册的时间
Date tnow = new Date();
String s = AlarmModel.getString(tnow);
//判断当前时间是否与注册时间相同
if(snow.equals(s)){
//相等,代表时PUSH注册唤醒了MIDlet
Form f = new Form("闹钟");
f.append("闹钟响起来咯");
f.addCommand(stopCommand);
f.setCommandListener(this);
//播放声音
model.playSound();
display.setCurrent(f);
return;
}
}
}
//非PUSH注册唤醒MIDlet情况下,显示主界面
display.setCurrent(form);
}
protected void pauseApp() {
}
protected void destroyApp(boolean arg0){
//释放资源
display = null;
model.release();
}
//响应用户动作
public void commandAction(Command cmd,Displayable displayable){
if(cmd == exitCommand){
destroyApp(true);
notifyDestroyed();
}else if(cmd == okCommand){
//在新线程中注册,避免堵塞。事实上在Nokia 7610中测试主线程注册也可以
set = date.getDate();
new Thread(new AlarmSetter()).start();
}else if(cmd == stopCommand){
model.stopSound();
display.setCurrent(form);
}
}
class AlarmSetter implements Runnable{
public void run(){
try {
//将闹钟设置时间存储到RMS中
model.writeDate(set);
//注册时钟
PushRegistry.registerAlarm(className,set.getTime());
Alert alert = new Alert("完成提示");
alert.setTimeout(2000);
alert.setString("您已经成功设置了闹钟");
alert.setType(AlertType.CONFIRMATION);
display.setCurrent(alert, form);
} catch(Exception e) {
e.printStackTrace();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -