📄 mainmidlet.java
字号:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package Main;import javax.microedition.midlet.*;import javax.microedition.lcdui.*;import javax.microedition.rms.*;import java.io.*;import java.util.Calendar;import java.util.Date;import java.util.TimerTask;import java.util.Timer;import javax.microedition.media.*;/** * @author HolyCat */public class MainMIDlet extends MIDlet implements CommandListener { private boolean midletPaused = false; //<editor-fold defaultstate="collapsed" desc=" Generated Fields ">//GEN-BEGIN:|fields|0| private Command exitCommand; private Command ChangeCommand; private Form form; private DateField dateField1; private StringItem stringItem; private DateField dateField; //</editor-fold>//GEN-END:|fields|0| // public class MyTimerTask extends TimerTask { public void run() { Ringing(System.currentTimeMillis()); } } // long NextDate; Timer timer=new Timer(); MyTimerTask MyTask=new MyTimerTask(); Player player; // public static long bytes2long(byte[] b) { int mask=0xff; long temp=0; long res=0; for(int i=0;i <8;i++){ res <<=8; temp=b[i]&mask; res|=temp; } return res; } public static byte[] long2bytes(long num) { byte[] b=new byte[8]; for(int i=0;i <8;i++){ b[i]=(byte)(num>>>(56-i*8)); } return b; } public void Ringing(long NowDate){ Date date=new Date(); try{ if (NowDate>NextDate){ date.setTime(NextDate); dateField1.setDate(date); player.start(); } date.setTime(NowDate); dateField.setDate(date); } catch(Exception e){ System.out.println("@@@@@@@@@@@@@@@@@@@@@"+e); } } /** * The MainMIDlet constructor. */ public MainMIDlet() { } //<editor-fold defaultstate="collapsed" desc=" Generated Methods ">//GEN-BEGIN:|methods|0| //</editor-fold>//GEN-END:|methods|0| //<editor-fold defaultstate="collapsed" desc=" Generated Method: initialize ">//GEN-BEGIN:|0-initialize|0|0-preInitialize /** * Initilizes the application. * It is called only once when the MIDlet is started. The method is called before the <code>startMIDlet</code> method. */ private void initialize() {//GEN-END:|0-initialize|0|0-preInitialize // write pre-initialize user code here//GEN-LINE:|0-initialize|1|0-postInitialize // write post-initialize user code here }//GEN-BEGIN:|0-initialize|2| //</editor-fold>//GEN-END:|0-initialize|2| //<editor-fold defaultstate="collapsed" desc=" Generated Method: startMIDlet ">//GEN-BEGIN:|3-startMIDlet|0|3-preAction /** * Performs an action assigned to the Mobile Device - MIDlet Started point. */ public void startMIDlet() {//GEN-END:|3-startMIDlet|0|3-preAction // write pre-action user code here switchDisplayable(null, getForm());//GEN-LINE:|3-startMIDlet|1|3-postAction // write post-action user code here //初始化上回闹铃记录 RecordStore RSMain = null; Date date; InputStream is; //默认初始时间2009年1月1日0点1分 Calendar cale=Calendar.getInstance(); cale.set(Calendar.YEAR, 2009); cale.set(Calendar.MONTH, Calendar.JANUARY); cale.set(Calendar.DATE,1); cale.set(Calendar.HOUR_OF_DAY,0); cale.set(Calendar.MINUTE,1); date=cale.getTime(); dateField1.setDate(date); long ldate=date.getTime(); byte[] bdate=long2bytes(ldate); try{ //打开读取上次响铃记录 RSMain = RecordStore.openRecordStore("RS",true,RecordStore.AUTHMODE_ANY ,false); if (RSMain.getNumRecords()==0){ RSMain.addRecord(bdate, 0, 8); } bdate=RSMain.getRecord(1); ldate=bytes2long(bdate); date.setTime(ldate); dateField1.setDate(date); //计算下回闹铃时间 do{ ldate+=172800000L; }while (ldate<System.currentTimeMillis ()); NextDate=ldate; date.setTime(ldate); stringItem.setText(date.toString()); //启动计时器 is=this.getClass().getResourceAsStream("/ring.wav"); player=Manager.createPlayer(is,"audio/x-wav"); //关闭 RSMain.closeRecordStore(); } catch(Exception e){System.out.print("@@@@@@@@@@@@@@@@@@@@@"+e);}; }//GEN-BEGIN:|3-startMIDlet|2| //</editor-fold>//GEN-END:|3-startMIDlet|2| //<editor-fold defaultstate="collapsed" desc=" Generated Method: resumeMIDlet ">//GEN-BEGIN:|4-resumeMIDlet|0|4-preAction /** * Performs an action assigned to the Mobile Device - MIDlet Resumed point. */ public void resumeMIDlet() {//GEN-END:|4-resumeMIDlet|0|4-preAction // write pre-action user code here//GEN-LINE:|4-resumeMIDlet|1|4-postAction // write post-action user code here }//GEN-BEGIN:|4-resumeMIDlet|2| //</editor-fold>//GEN-END:|4-resumeMIDlet|2| //<editor-fold defaultstate="collapsed" desc=" Generated Method: switchDisplayable ">//GEN-BEGIN:|5-switchDisplayable|0|5-preSwitch /** * Switches a current displayable in a display. The <code>display</code> instance is taken from <code>getDisplay</code> method. This method is used by all actions in the design for switching displayable. * @param alert the Alert which is temporarily set to the display; if <code>null</code>, then <code>nextDisplayable</code> is set immediately * @param nextDisplayable the Displayable to be set */ public void switchDisplayable(Alert alert, Displayable nextDisplayable) {//GEN-END:|5-switchDisplayable|0|5-preSwitch // write pre-switch user code here Display display = getDisplay();//GEN-BEGIN:|5-switchDisplayable|1|5-postSwitch if (alert == null) { display.setCurrent(nextDisplayable); } else { display.setCurrent(alert, nextDisplayable); }//GEN-END:|5-switchDisplayable|1|5-postSwitch // write post-switch user code here }//GEN-BEGIN:|5-switchDisplayable|2| //</editor-fold>//GEN-END:|5-switchDisplayable|2| //<editor-fold defaultstate="collapsed" desc=" Generated Method: commandAction for Displayables ">//GEN-BEGIN:|7-commandAction|0|7-preCommandAction /** * Called by a system to indicated that a command has been invoked on a particular displayable. * @param command the Command that was invoked * @param displayable the Displayable where the command was invoked */ public void commandAction(Command command, Displayable displayable) {//GEN-END:|7-commandAction|0|7-preCommandAction // write pre-action user code here if (displayable == form) {//GEN-BEGIN:|7-commandAction|1|24-preAction if (command == ChangeCommand) {//GEN-END:|7-commandAction|1|24-preAction // write pre-action user code here RecordStore RSMain = null; Date date; long ldate; byte[] bdate; try{ RSMain = RecordStore.openRecordStore("RS",true,RecordStore.AUTHMODE_ANY ,false); date=dateField1.getDate(); ldate=date.getTime(); bdate=long2bytes(ldate); RSMain.setRecord(1, bdate, 0, 8); //计算下回闹铃时间 do{ ldate+=172800000L; }while (ldate<System.currentTimeMillis ()); NextDate=ldate; date.setTime(ldate); stringItem.setText(date.toString()); player.stop(); } catch(Exception e){System.out.print(e);}; //Ringing(0);//GEN-LINE:|7-commandAction|2|24-postAction // write post-action user code here } else if (command == exitCommand) {//GEN-LINE:|7-commandAction|3|19-preAction // write pre-action user code here Display display = getDisplay(); display.setCurrent(null);//GEN-LINE:|7-commandAction|4|19-postAction // write post-action user code here }//GEN-BEGIN:|7-commandAction|5|7-postCommandAction }//GEN-END:|7-commandAction|5|7-postCommandAction // write post-action user code here }//GEN-BEGIN:|7-commandAction|6| //</editor-fold>//GEN-END:|7-commandAction|6| //<editor-fold defaultstate="collapsed" desc=" Generated Getter: exitCommand ">//GEN-BEGIN:|18-getter|0|18-preInit /** * Returns an initiliazed instance of exitCommand component. * @return the initialized component instance */ public Command getExitCommand() { if (exitCommand == null) {//GEN-END:|18-getter|0|18-preInit // write pre-init user code here exitCommand = new Command("\u8F6C\u81F3\u540E\u53F0", Command.EXIT, 0);//GEN-LINE:|18-getter|1|18-postInit // write post-init user code here }//GEN-BEGIN:|18-getter|2| return exitCommand; } //</editor-fold>//GEN-END:|18-getter|2| //<editor-fold defaultstate="collapsed" desc=" Generated Getter: form ">//GEN-BEGIN:|14-getter|0|14-preInit /** * Returns an initiliazed instance of form component. * @return the initialized component instance */ public Form getForm() { if (form == null) {//GEN-END:|14-getter|0|14-preInit // write pre-init user code here form = new Form("48Clock", new Item[] { getDateField1(), getStringItem(), getDateField() });//GEN-BEGIN:|14-getter|1|14-postInit form.addCommand(getExitCommand()); form.addCommand(getChangeCommand()); form.setCommandListener(this);//GEN-END:|14-getter|1|14-postInit // write post-init user code here }//GEN-BEGIN:|14-getter|2| return form; } //</editor-fold>//GEN-END:|14-getter|2| //<editor-fold defaultstate="collapsed" desc=" Generated Getter: dateField1 ">//GEN-BEGIN:|22-getter|0|22-preInit /** * Returns an initiliazed instance of dateField1 component. * @return the initialized component instance */ public DateField getDateField1() { if (dateField1 == null) {//GEN-END:|22-getter|0|22-preInit // write pre-init user code here dateField1 = new DateField("\u4E0A\u6B21\u95F9\u94C3\u65F6\u95F4", DateField.DATE_TIME);//GEN-BEGIN:|22-getter|1|22-postInit dateField1.setDate(new java.util.Date(System.currentTimeMillis()));//GEN-END:|22-getter|1|22-postInit // write post-init user code here }//GEN-BEGIN:|22-getter|2| return dateField1; } //</editor-fold>//GEN-END:|22-getter|2| //<editor-fold defaultstate="collapsed" desc=" Generated Getter: ChangeCommand ">//GEN-BEGIN:|23-getter|0|23-preInit /** * Returns an initiliazed instance of ChangeCommand component. * @return the initialized component instance */ public Command getChangeCommand() { if (ChangeCommand == null) {//GEN-END:|23-getter|0|23-preInit // write pre-init user code here ChangeCommand = new Command("\u4FEE\u6539\\\u6D88\u97F3", Command.ITEM, 0);//GEN-LINE:|23-getter|1|23-postInit // write post-init user code here }//GEN-BEGIN:|23-getter|2| return ChangeCommand; } //</editor-fold>//GEN-END:|23-getter|2| //<editor-fold defaultstate="collapsed" desc=" Generated Getter: stringItem ">//GEN-BEGIN:|26-getter|0|26-preInit /** * Returns an initiliazed instance of stringItem component. * @return the initialized component instance */ public StringItem getStringItem() { if (stringItem == null) {//GEN-END:|26-getter|0|26-preInit // write pre-init user code here stringItem = new StringItem("\u4E0B\u6B21\u95F9\u94C3\u65F6\u95F4", "2009-01-01 00:01", Item.PLAIN);//GEN-LINE:|26-getter|1|26-postInit // write post-init user code here }//GEN-BEGIN:|26-getter|2| return stringItem; } //</editor-fold>//GEN-END:|26-getter|2| //<editor-fold defaultstate="collapsed" desc=" Generated Getter: dateField ">//GEN-BEGIN:|27-getter|0|27-preInit /** * Returns an initiliazed instance of dateField component. * @return the initialized component instance */ public DateField getDateField() { if (dateField == null) {//GEN-END:|27-getter|0|27-preInit // write pre-init user code here dateField = new DateField("\u5F53\u524D\u7CFB\u7EDF\u65F6\u95F4", DateField.DATE_TIME);//GEN-BEGIN:|27-getter|1|27-postInit dateField.setDate(new java.util.Date(System.currentTimeMillis()));//GEN-END:|27-getter|1|27-postInit // write post-init user code here }//GEN-BEGIN:|27-getter|2| return dateField; } //</editor-fold>//GEN-END:|27-getter|2| /** * Returns a display instance. * @return the display instance. */ public Display getDisplay () { return Display.getDisplay(this); } /** * Exits MIDlet. */ public void exitMIDlet() { switchDisplayable (null, null); destroyApp(true); notifyDestroyed(); } /** * Called when MIDlet is started. * Checks whether the MIDlet have been already started and initialize/starts or resumes the MIDlet. */ public void startApp() { if (midletPaused) { resumeMIDlet (); } else { initialize (); startMIDlet (); } midletPaused = false; timer.schedule(MyTask, 100,60000); } /** * Called when MIDlet is paused. */ public void pauseApp() { midletPaused = true; } /** * Called to signal the MIDlet to terminate. * @param unconditional if true, then the MIDlet has to be unconditionally terminated and all resources has to be released. */ public void destroyApp(boolean unconditional) { }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -