无花的空间--http://wuhua.3geye.net-- 您今天UCWEB了吗?--http://www.ucweb.com

当你来到我的空间的时候,你会看到很多Google,Nokia,Mobile,J2ME ,Android,3G,无线开发,手机软件,手机软件开发等与Google,手机相关的文章,你千万不要惊讶。这就是我一个专注Google,无线开发的无花。传播知识,分享经验,技术交流是我开空间的直接目的。当然如果有项目开发,我还是很希望赚点外快的。

用户操作
[即时聊天] [发私信] [加为好友]
无花ID:gooogledev
38106次访问,排名3032好友11人,关注者18
J2EE 2年的开发时间,
J2ME 2年的开发时间
gooogledev的文章
原创 78 篇
翻译 0 篇
转载 3 篇
评论 201 篇
无花的公告

人要吃很多苦才可以长大!

踏实平凡人应该遵守的原则!

与我TM

gooogledev@gmail.com

友情链接

我的j2me创意

Wap浏览器的源代码

用自己写的rms引擎写的电话本

RSS订阅此博客  
用抓虾订阅此博客
用google订阅此博客
用bloglines订阅此博客
    网络封神榜 | 饶荣庆
最近评论
leo2012:奇怪,怎么下的wap explorer不能访问wap站点
wjw334:谢谢分享
liu:liuwenbin.cn@gmail.com
发一个xdoclet-1.2.1.jar,谢谢
yg:
[url=http://www.item4trade.com/]wow gold[/url]
[url=http://www.item4trade.com/]power leveling[/url]
[url=http://www.item4trade.com/]powerleveling[/url]
[url=http://www.item4tra……
weiguochuli:if(flags) flags可是boolean型的数组啊,报错的!还有如果选中了两个值但images.size() 的值是3的话第一个值会得到两遍的。
文章分类
收藏
    相册
    j2me创意图片
    j2me教程
    我的照片
    我的链接
    3G视线
    EasyMF J2ME框架
    Lucene中国
    我的Google
    无花的博客
    存档
    软件项目交易
    订阅我的博客
    XML聚合  FeedSky
    订阅到鲜果
    订阅到Google
    订阅到抓虾
    订阅到BlogLines
    订阅到Yahoo
    订阅到GouGou
    订阅到飞鸽
    订阅到Rojo
    订阅到newsgator
    订阅到netvibes

    原创 用一个简单的例子来看MIDlet的生命周期收藏

    新一篇: 基于j2me xml解释 引擎 XmlPullParser 的例子 | 旧一篇: J2ME的概述,结构,现在与未来,以及我能用J2ME做什么?

    用一个简单的例子来看MIDlet的生命周期

    想来估计也没有比网上教程说的更清楚了,我这里摘录的只是文字,从www.j2medev.com来获取,更详细的资料可以到www.j2medev.com上查看。我将会以一个例子跟查看官方的源代码来分析它们。

    理解J2ME 的体系结构并不像想象的那么容易,我们觉得读更多的资料帮助也不大,我们

    直接迈向J2ME 开发也许会对你理解J2ME 平台体系结构这个重要的概念有所帮助。在MIDP

    中定义了一种新的应用程序模型MIDlet,它是被Application Management SoftwareAMS)管理

    的。AMS 负责MIDlet 的安装、下载、运行和删除等操作。在被AMS 管理的同时,MIDlet

    以和应用管理软件通信通知应用管理软件自己状态的变化,通常是通过方法notifyDestroyed()

    notifyPaused()实现的

    MIDlet 有三个状态,分别是pauseactive 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公司帮我们做了很多事情,让我们的程序更好的开发与管理。

    发表于 @ 2007年05月08日 10:23:00|评论(loading...)|编辑

    新一篇: 基于j2me xml解释 引擎 XmlPullParser 的例子 | 旧一篇: J2ME的概述,结构,现在与未来,以及我能用J2ME做什么?

    评论:没有评论。

    发表评论  


    登录
    Csdn Blog version 3.1a
    Copyright © 无花