精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2007-05-09
用一个简单的例子来看MIDlet的生命周期 用一个简单的例子来看MIDlet 的生命周期 想来估计也没有比网上教程说的更清楚了,我这里摘录的只是文字,从www.j2medev.com来获取,更详细的资料可以到www.j2medev.com上查看。我将会以一个例子跟查看官方的源代码来分析它们。 理解J2ME 的体系结构并不像想象的那么容易,我们觉得读更多的资料帮助也不大,我们 直接迈向J2ME 开发也许会对你理解J2ME 平台体系结构这个重要的概念有所帮助。在MIDP 中定义了一种新的应用程序模型MIDlet,它是被Application Management Software(AMS)管理 的。AMS 负责MIDlet 的安装、下载、运行和删除等操作。在被AMS 管理的同时,MIDlet 可 以和应用管理软件通信通知应用管理软件自己状态的变化,通常是通过方法notifyDestroyed() 和notifyPaused()实现的 MIDlet 有三个状态,分别是pause、active 和destroyed。在启动一个MIDlet 的时候,应用 管理软件会首先创建一个MIDlet 实例并使得他处于pause 状态,当startApp()方法被调用的时候 MIDlet 进入active 状态,也就是所说的运行状态。在active 状态调用destroyApp(boolean 第1 章 J2ME 技术概述 4 unconditional)或者pauseApp()方法可以使得MIDlet 进入destroyed 或者pause 状态。值得一提的 是destroyApp(boolean unconditional)方法,事实上,当destroyApp()方法被调用的时候,AMS 通 知MIDlet 进入destroyed 状态。在destroyed 状态的MIDlet 必须释放了所有的资源,并且保存了 数据。如果unconditional 为false 的时候, MIDlet 可以在接到通知后抛出 MIDletStateChangeException 而保持在当前状态,如果设置为true 的话,则必须立即进入destroyed 状态。下图说明了MIDlet 状态改变情况: <!----><!----><!----> 看看我那个简单的例子 public class HelloWorld extends MIDlet ......{
public HelloWorld() ......{ System.out.println("这个是程序的构造函数,程序运行的时候首先调用这个"); } protected void destroyApp(boolean unconditional) throws MIDletStateChangeException ......{ System.out.println("这个是程序的destroyed事件,当您按下退出时调用"); } protected void pauseApp() ......{ System.out.println("这个是程序的pause事件,当您按下暂停的时调用"); } protected void startApp() throws MIDletStateChangeException ......{ System.out.println("这个是程序的active事件,程序启动时候调用"); } } 大家可以运行程序中看到这个程序的运行先后顺些。基本上就明白了程序的调用机制了。 现在大家思考下,j2me的MIDlet是怎么样运行的呢?sun在里面进行了什么样子的限制与手脚呢? 一般的应用程序都有个main入门。这里没有,为什么呢? 我想这个就是ASM的作用了,sun在后台做了很多处理,比如包括,启动容器,启动MIDlet相关的资源等等。 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"); &nb 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
浏览 5093 次