饒榮慶 -- 您今天UCWEB了嗎?--http://www.ucweb.com

          3G 手機(jī)開發(fā)網(wǎng)

             :: 首頁(yè) :: 聯(lián)系 :: 聚合  :: 管理
            99 Posts :: 1 Stories :: 219 Comments :: 0 Trackbacks
          原文
          用一個(gè)簡(jiǎn)單的例子來(lái)看MIDlet的生命周期

          用一個(gè)簡(jiǎn)單的例子來(lái)看MIDlet 的生命周期

          想來(lái)估計(jì)也沒(méi)有比網(wǎng)上教程說(shuō)的更清楚了,我這里摘錄的只是文字,從www.j2medev.com來(lái)獲取,更詳細(xì)的資料可以到www.j2medev.com上查看。我將會(huì)以一個(gè)例子跟查看官方的源代碼來(lái)分析它們。

          理解J2ME 的體系結(jié)構(gòu)并不像想象的那么容易,我們覺得讀更多的資料幫助也不大,我們

          直接邁向J2ME 開發(fā)也許會(huì)對(duì)你理解J2ME 平臺(tái)體系結(jié)構(gòu)這個(gè)重要的概念有所幫助。在MIDP

          中定義了一種新的應(yīng)用程序模型MIDlet,它是被Application Management Software(AMS)管理

          的。AMS 負(fù)責(zé)MIDlet 的安裝、下載、運(yùn)行和刪除等操作。在被AMS 管理的同時(shí),MIDlet 可

          以和應(yīng)用管理軟件通信通知應(yīng)用管理軟件自己狀態(tài)的變化,通常是通過(guò)方法notifyDestroyed()

          和notifyPaused()實(shí)現(xiàn)的

          MIDlet 有三個(gè)狀態(tài),分別是pause、active 和destroyed。在啟動(dòng)一個(gè)MIDlet 的時(shí)候,應(yīng)用

          管理軟件會(huì)首先創(chuàng)建一個(gè)MIDlet 實(shí)例并使得他處于pause 狀態(tài),當(dāng)startApp()方法被調(diào)用的時(shí)候

          MIDlet 進(jìn)入active 狀態(tài),也就是所說(shuō)的運(yùn)行狀態(tài)。在active 狀態(tài)調(diào)用destroyApp(boolean

          第1 章 J2ME 技術(shù)概述

          4

          unconditional)或者pauseApp()方法可以使得MIDlet 進(jìn)入destroyed 或者pause 狀態(tài)。值得一提的

          是destroyApp(boolean unconditional)方法,事實(shí)上,當(dāng)destroyApp()方法被調(diào)用的時(shí)候,AMS 通

          知MIDlet 進(jìn)入destroyed 狀態(tài)。在destroyed 狀態(tài)的MIDlet 必須釋放了所有的資源,并且保存了

          數(shù)據(jù)。如果unconditional 為false 的時(shí)候, MIDlet 可以在接到通知后拋出

          MIDletStateChangeException 而保持在當(dāng)前狀態(tài),如果設(shè)置為true 的話,則必須立即進(jìn)入destroyed

          狀態(tài)。下圖說(shuō)明了MIDlet 狀態(tài)改變情況:

           


          看看我那個(gè)簡(jiǎn)單的例子
          public class HelloWorld extends MIDlet ......{

              public HelloWorld() ......{ 
                  System.out.println("這個(gè)是程序的構(gòu)造函數(shù),程序運(yùn)行的時(shí)候首先調(diào)用這個(gè)");
              }

              protected void destroyApp(boolean unconditional)
                      throws MIDletStateChangeException ......{
                  System.out.println("這個(gè)是程序的destroyed事件,當(dāng)您按下退出時(shí)調(diào)用");
              }

              protected void pauseApp() ......{
                  System.out.println("這個(gè)是程序的pause事件,當(dāng)您按下暫停的時(shí)調(diào)用");

              }

              protected void startApp() throws MIDletStateChangeException ......{
                  System.out.println("這個(gè)是程序的active事件,程序啟動(dòng)時(shí)候調(diào)用");

              }

          }

          大家可以運(yùn)行程序中看到這個(gè)程序的運(yùn)行先后順些。基本上就明白了程序的調(diào)用機(jī)制了。
          現(xiàn)在大家思考下,j2me的MIDlet是怎么樣運(yùn)行的呢?sun在里面進(jìn)行了什么樣子的限制與手腳呢?
          一般的應(yīng)用程序都有個(gè)main入門。這里沒(méi)有,為什么呢?
          我想這個(gè)就是ASM的作用了,sun在后臺(tái)做了很多處理,比如包括,啟動(dòng)容器,啟動(dòng)MIDlet相關(guān)的資源等等。

          public static void main(String args[]) ...{
                  CommandState state = new CommandState();

              /**//*
               * pass resource strings down to the native system menu and
               * popup choice group methods...
               */
              initSystemLabels();

                  /**//*
                   * We will try to handle any printing at this level, because
                   * displaying JAM command line errors is device specific.
                   */
                  try ...{
                      initializeInternalSecurity();

                  /**//* Start a inbound connection watcher thread. */
                  new Thread(new PushRegistryImpl()).start();

                      restoreCommandState(state);

                      // handle any development machine only functions at this level
                      switch (state.nextCommand) ...{
                      case CommandProcessor.RUN_CLASS:
                          runLocalClass(state);
                          state.nextCommand = CommandProcessor.EXIT;
                          break;

                      case CommandProcessor.MANAGE:
                          manage(state);
                          break;

                      case CommandProcessor.LIST:
                      case CommandProcessor.STORAGE_NAMES:
                          list(state);
                          state.nextCommand = CommandProcessor.EXIT;
                          break;

                      case CommandProcessor.REMOVE:
                          if (DEV_STORAGE_NAME.equals(state.suiteStorageName)) ...{
                              removeDevStorage(state);
                              state.nextCommand = CommandProcessor.EXIT;
                              break;
                          }

                          // fall through
                      default:
                          CommandProcessor.perform(state);
                          if (state.status == CommandProcessor.MIDLET_SUITE_NOT_FOUND) ...{
                              System.out.println("The MIDlet suite was not found.");
                          } else if (state.initialCommand == CommandProcessor.INSTALL &&
                                  state.status == CommandProcessor.OK) ...{
                              System.out.println("Storage name: " +
                                                 state.suiteStorageName);
                          }
                      }
                  } catch (InvalidJadException ije) ...{
                      System.out.println("** Error installing suite (" +
                                         ije.getReason() + "): " + 
                                         messageForInvalidJadException(ije));
                  } catch (IOException ioe) ...{
                      System.out.println("** Error installing suite: " +
                                         ioe.getMessage());
                  } catch (ClassNotFoundException ex) ...{
                      if (state.initialCommand == CommandProcessor.MANAGE) ...{

                        state.runExceptionMessage =
                              Resource.getString("The application cannot be launched. " +
                              "One of the application classes appears to be missing. " +
                              "This could be due to a mis-named class. Contact the " +
                              "application provider to resolve the issue.");
                      } else ...{
                          System.out.println("MIDlet class(s) not found: " + 
                                             ex.getMessage());
                      }
                  } catch (InstantiationException ex) ...{
                      if (state.initialCommand == CommandProcessor.MANAGE) ...{
                         state.runExceptionMessage = Resource.getString(
                             "The application cannot be launched. The application " +
                             "may have done an illegal operation. Contact the " +
                             "application provider to resolve the issue.") + " " +
                             ex.getMessage();
                      } else ...{
                          System.out.println(
                              "MIDlet instance(s) could not be created: " + 
                                           ex.getMessage());
                      }
                  } catch (IllegalAccessException ex) ...{
                      if (state.initialCommand == CommandProcessor.MANAGE) ...{
                          state.runExceptionMessage = Resource.getString(
                             "The application cannot be launched. The application " +
                             "may have done an illegal operation. Contact the " +
                             "application provider to resolve the issue.") + " " +
                             ex.getMessage();
                      } else ...{
                          System.out.println(
                              "MIDlet class(s) could not be accessed: " + 
                              ex.getMessage());
                      }
                  } catch (OutOfMemoryError ex) ...{
                      if (state.initialCommand == CommandProcessor.MANAGE) ...{
                          state.runExceptionMessage = Resource.getString(
                              "The application has unexpectedly quit because it ran " +
                              "out of memory.");
                      } else ...{
                          System.out.println("The MIDlet has run out of memory");
                      }
                  } catch (IllegalArgumentException ex) ...{
                      System.out.println(ex.getMessage());
                  } catch (Throwable t) ...{
                      if (state.initialCommand == CommandProcessor.MANAGE) ...{
                         state.runExceptionMessage =
                              Resource.getString("The application has unexpectedly " +
                              " quit. Contact the application provider to resolve " +
                              "the issue.") + " " + t.getMessage();
                      } else ...{
                          System.out.println("Exception caught in main:");
                          t.printStackTrace();
                          state.nextCommand = CommandProcessor.EXIT;
                      }
                  }

                  saveCommandState(state);

                  /**//*
                   * return any non-zero number so the native main can know that
                   * this is graceful exit and not the power button on the phone.
                   */
                  exitInternal(CommandProcessor.MAIN_EXIT);
              }

          這個(gè)是從j2me底層源代碼上找到的,關(guān)于main入口方法的一段代碼。由此可以知道,j2me也是有main方法的,只是sun公司幫我們做了很多事情,讓我們的程序更好的開發(fā)與管理。

          更加詳細(xì)的文章請(qǐng)看
          深入理解MIDlet類
          深入了解MIDP-基礎(chǔ)篇


          爬蟲工作室 -- 專業(yè)的手機(jī)軟件開發(fā)工作室
          3G視線 -- 專注手機(jī)軟件開發(fā)
          posted on 2007-05-09 08:40 3G工作室 閱讀(1499) 評(píng)論(2)  編輯  收藏 所屬分類: j2me 入門

          Feedback

          # re: 用一個(gè)簡(jiǎn)單的例子來(lái)看MIDlet的生命周期 2007-05-09 09:18 JX
          朋友,這里會(huì)讓你動(dòng)心的,過(guò)來(lái)看看

          大多數(shù)人都日復(fù)一日地重復(fù)著朝九晚五的生活,我們處于一種“做一天和尚撞一天鐘”的狀態(tài),只是為了賺取按月計(jì)酬的工資--循規(guī)蹈矩工作,勤勤懇懇賺錢!這種生存方式,長(zhǎng)久以來(lái),禁錮著我們的頭腦!

            許多人都想通過(guò)網(wǎng)絡(luò)尋求更多的發(fā)展機(jī)會(huì),互聯(lián)網(wǎng)為您展現(xiàn)了一個(gè)廣闊的空間,您可在其中尋求發(fā)展,實(shí)現(xiàn)自己的創(chuàng)業(yè)夢(mèng)想!您的成功無(wú)需朝九晚五,這里每個(gè)人的機(jī)會(huì)都是均等的,您可在網(wǎng)絡(luò)世界里大展宏圖!

            耽誤您10分鐘時(shí)間,登陸我的網(wǎng)站:

            http://jx.cctve.cn
          QQ:79147981

            給我一次關(guān)注,回報(bào)您一個(gè)成功的機(jī)會(huì)!讓您的人生從此與眾不同、精彩紛呈!  回復(fù)  更多評(píng)論
            

          # re: 用一個(gè)簡(jiǎn)單的例子來(lái)看MIDlet的生命周期 2007-05-09 22:44 二手車
          這個(gè)Blog不簡(jiǎn)單啊。  回復(fù)  更多評(píng)論
            

          主站蜘蛛池模板: 博乐市| 墨脱县| 石狮市| 平凉市| 信宜市| 板桥市| 湖州市| 贵溪市| 西林县| 辽源市| 元谋县| 淳化县| 安顺市| 建始县| 祁东县| 英山县| 会昌县| 武鸣县| 江油市| 长沙市| 东丰县| 图们市| 朔州市| 阿克陶县| 屯昌县| 余干县| 房产| 吉安县| 同德县| 淅川县| 商南县| 盱眙县| 武威市| 原平市| 乐昌市| 陆河县| 剑川县| 炉霍县| 墨玉县| 乾安县| 丰城市|