饶荣庆 -- 您今天UCWEB了吗?--http://www.ucweb.com

3G 手机开发网

   :: 首页 :: 联系 :: 聚合  :: 管理
  99 Posts :: 1 Stories :: 219 Comments :: 0 Trackbacks
原文
用一个简单的例子来看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");
            }
        } 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);
    }

这个是从j2me底层源代码上找到的,关于main入口方法的一段代码。由此可以知道,j2me也是有main方法的,只是sun公司帮我们做了很多事情,让我们的程序更好的开发与管理。

更加详细的文章请看
深入理解MIDlet类
深入了解MIDP-基础篇


爬虫工作室 -- 专业的手机软件开发工作室
3G视线 -- 专注手机软件开发
posted on 2007-05-09 08:40 3G工作室 阅读(1474) 评论(2)  编辑  收藏 所属分类: j2me 入门

Feedback

# re: 用一个简单的例子来看MIDlet的生命周期 2007-05-09 09:18 JX
朋友,这里会让你动心的,过来看看

大多数人都日复一日地重复着朝九晚五的生活,我们处于一种“做一天和尚撞一天钟”的状态,只是为了赚取按月计酬的工资--循规蹈矩工作,勤勤恳恳赚钱!这种生存方式,长久以来,禁锢着我们的头脑!

  许多人都想通过网络寻求更多的发展机会,互联网为您展现了一个广阔的空间,您可在其中寻求发展,实现自己的创业梦想!您的成功无需朝九晚五,这里每个人的机会都是均等的,您可在网络世界里大展宏图!

  耽误您10分钟时间,登陆我的网站:

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

  给我一次关注,回报您一个成功的机会!让您的人生从此与众不同、精彩纷呈!  回复  更多评论
  

# re: 用一个简单的例子来看MIDlet的生命周期 2007-05-09 22:44 二手车
这个Blog不简单啊。  回复  更多评论
  


只有注册用户登录后才能发表评论。


网站导航: