張生工作室

          一切皆有可能

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            24 隨筆 :: 3 文章 :: 11 評論 :: 0 Trackbacks
          <2007年9月>
          2627282930311
          2345678
          9101112131415
          16171819202122
          23242526272829
          30123456

          常用鏈接

          留言簿(4)

          隨筆檔案

          文章檔案

          相冊

          好友鏈接

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

           

          完成最簡單的鬧鈴功能 
          /*
          * 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)  編輯  收藏

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


          網站導航:
           
          主站蜘蛛池模板: 壶关县| 宁海县| 黄浦区| 九龙县| 富顺县| 贵港市| 泰州市| 贵定县| 乳源| 航空| 金华市| 兰西县| 图们市| 龙门县| 随州市| 南岸区| 遂溪县| 望江县| 浙江省| 九台市| 繁峙县| 历史| 石棉县| 三明市| 专栏| 武宣县| 方山县| 沙洋县| 靖州| 梁山县| 岫岩| 榆树市| 郴州市| 望谟县| 文水县| 卢氏县| 紫金县| 永宁县| 怀柔区| 凤城市| 无锡市|