⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 alarmmidlet.java

📁 <j2me 开发精解> 詹建光著 里所有的源码。对J2me的开发相当有帮助
💻 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){//第一次启动还没有注册
                Date tnow = new Date();
                String s = AlarmModel.getString(tnow);
                if(snow.equals(s)){//闹钟
                    Form af = new Form("闹钟");
                    af.append("闹钟响起来咯");
                    af.addCommand(stopCommand);
                    af.setCommandListener(this);
                    model.playSound();
                    display.setCurrent(af);
                    return;
                }
            }

        }
        display.setCurrent(form);
        
    }
    protected void pauseApp() {
        
        
    }
    protected void destroyApp(boolean arg0){
        display = null;
    }
    
    private void scheduleMIDlet(Date d) {
        try {
            model.writeDate(d);
            PushRegistry.registerAlarm(className,d.getTime());
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
    
    public void commandAction(Command cmd,Displayable displayable){
        if(cmd == exitCommand){
            destroyApp(true);
            notifyDestroyed();
        }else if(cmd == okCommand){
            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 {
                model.writeDate(set);
                PushRegistry.registerAlarm(className,set.getTime());
                Alert alert = new Alert("闹钟设置完成");
                alert.setTimeout(2000);
                alert.setType(AlertType.CONFIRMATION);
                display.setCurrent(alert, form);
            } catch(Exception e) {
                e.printStackTrace();
            }
        }
    }
    
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -