張生工作室

          一切皆有可能

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            24 隨筆 :: 3 文章 :: 11 評論 :: 0 Trackbacks

           

          完成最簡單的鬧鈴功能 
          /*
          * Midlet.java
          *
          * Created on 2007年9月8日, 下午12:24
          */


          package hello;

          import java.util.Date;
          import java.util.Timer;
          import java.util.TimerTask;
          import javax.microedition.midlet.*;
          import javax.microedition.lcdui.*;

          /**
          *
          @author    Administrator
          @version
          */

          public class Midlet extends MIDlet implements javax.microedition.lcdui.CommandListener,javax.microedition.lcdui.ItemStateListener {
                
          private     Display display;
                 
          private     Form form;
                 
          private DateField setAlarm;
                 Alert alert;
                 Command exitCmd;
                 Command cancelCmd;
                 Timer alarmTimer;
                 MyTimerTask alarmTask;
                 Date current;
                 
          boolean firstTime=true;
              
                
          public Midlet()
                
          {
                      display
          =Display.getDisplay(this);
                      form
          =new Form("設定鬧鈴");
                      alert 
          =new Alert("");
                      alert.setTimeout(Alert.FOREVER);
                      form.append(
          "這是我第一次開發手機程序");
                      alert.setString(
          "時間到了");
                    
                      setAlarm
          =new DateField("設定時間",DateField.DATE_TIME);
                      exitCmd
          =new Command("退出",Command.EXIT,1);
                      cancelCmd
          =new Command("關閉鈴聲",Command.CANCEL,1);
                      current
          =new Date();
                      alarmTimer
          =new Timer();
                      alarmTask
          =new MyTimerTask();
                      form.append(setAlarm);
                      form.setCommandListener(
          this);
                      form.addCommand(exitCmd);
                      form.addCommand(cancelCmd);
                      form.setItemStateListener(
          this);
                      alarmTimer
          =new Timer();
                      alarmTask
          =new MyTimerTask();
                    
                    
                }

                
          public void startApp() {
                    display
          =Display.getDisplay(this);
                    display.setCurrent(form);
                  
                  
                
                }

              
                
          public void pauseApp() {
                }

              
                
          public void destroyApp(boolean unconditional) {
                }


                
          public void commandAction(Command command, Displayable displayable) {
                  
                    
          if (command==exitCmd)
                    
          {
                        destroyApp(
          false);
                        notifyDestroyed();
                    }

                    
          else
                        
          if(command==cancelCmd)
                        
          {
                        alarmTimer.cancel();
                        }

                }


                
          public void itemStateChanged(Item item) {
                    Date setting
          =setAlarm.getDate();
                    
          if (setting.getTime()>current.getTime())
                    
          {
                        alarmTimer.scheduleAtFixedRate(alarmTask,setting,
          2000);
                    }

                }

              
                
          class MyTimerTask extends TimerTask
          {
               
          public void run()
               
          {
                  
          if (firstTime)
                  
          {
                      display.setCurrent(alert,form);
                      firstTime
          =false;
                  }

                  AlertType.CONFIRMATION.playSound(display);
               }

             
          }


          }



          張生工作室
          posted on 2007-09-08 21:27 張生工作室 閱讀(213) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 车致| 安阳市| 镇康县| 维西| 古浪县| 南木林县| 吉安县| 馆陶县| 襄城县| 色达县| 中江县| 轮台县| 花莲市| 凤庆县| 泸定县| 黎川县| 明溪县| 昌图县| 海城市| 报价| 加查县| 滁州市| 临漳县| 修文县| 营口市| 文登市| 三都| 孙吴县| 定安县| 关岭| 乾安县| 信阳市| 新竹县| 满洲里市| 许昌县| 田林县| 广州市| 南雄市| 弥勒县| 贺州市| 青州市|