Learning Path: MIDlet Life Cycle

来源:互联网 发布:成都淘宝客服招聘 编辑:程序博客网 时间:2024/05/12 14:20

原文地址:http://developers.sun.com/mobility/learn/midp/lifecycle/

Ps:有些简单的,不重要的就偷懒啦,哈哈

by Richard Marejka

February 2005

 

Welcome to the MIDlet Life-Cycle Learning Path! Here you'll find a brief introduction to the life-cycle of an application based on the Java 2 Platform, Micro Edition (J2ME), and links you can follow to articles, sample code, and specifications that will give you a solid grounding in this crucial area of J2ME-based development.

 

The MIDlet Life-Cycle learning path is a bit different from others in format. Many simply state goals and prerequisites briefly, then provide links to the content. This one provides the basic content directly, and along the way furnishes links to other resources.

 

This learning path will focus on source code. Why? Ultimately in a product life-cycle, someone must actually create an instantiation of a design that was based an architecture that was begotten by an idea. Business cases, designs, and architectures cannot be compiled to run on devices. Someone actually has to produce code that embodies the design, meets high standards of quality, and meets users' needs. This coding task includes crafting a build environment, managing a source base, and serving it all up in a form that allows the the developers to crank out the product in its deliverable form, on demand. Control and repeatability are key elements of software product management.

 

So where does all this lead? Virtually all source code is layered on top of existing application progamming interfaces (APIs). How well you understand the APIs you consume, and how correctly you use them, directly affect the quality of your product. Merely reading the API specifications isn't enough. Code that reflects a deep understanding of the API, and that demonstrates correct patterns of use, both reinforce the specification and provide idioms that can be directly employed by other developers. The purpose of this learning path is to help you produce that kind of code.

 

Introduction

JADs and JARs

The Security Model

Installation

Removal

The First Program

Execution States

Sample Source

Summary

 

Introduction

理解MIFlet的生命周期是创建MIDlet的基础。生命周期了一个MIDlet的的执行状态(创建、开始、停止、退出)和状态间的转换。AMS是设备上管理MIDlets下载和生命周期的软件。AMS提供了MIDlet的运行时环境。它安全、许可和执行状态进行强制管理,并且提供系统类和安排。

JADs and JARs

对于一个想要发布的MIDlet suite来说,JADJAR是基本的组件。JADJAR一起组成MIDlet suite

JAD文件,顾名思义是用来描述MIDlet suite的。描述包括MIDlet suite 名、JAR文件的大小和位置、配置和需要的条件。文件可能也包含其他属性,这些属性可能包含MIDP定义的属性或者开发者定义的属性,或者二者皆有。AMS预留了由MIDlet或者MicroEdition开始的属性。JAD文件与J2SE环境中java.util.Properties类的语法相近。

MIDlet suite 靠属性元组(MIDlet-Name, MIDlet-Version, MIDlet-Vendor)识别。JAR文件从MIDlet-Jar-URL处安装。下载文件的大小必须 MIDlet-Jar-Size值一致。

JAR包含一个或多个MIDlets,在JAD中用MIDlet-<n>属性指定不同的MIDlet

语法如下:

MIDlet-n : MIDletName , [IconPathname] , ClassName

其中:

n:                        MIDlet编号,由1开始,以1为单位增加

MIDletName   用户可见的MIDlet

IconPathnameJAR中后缀为.png图片的绝对路径

ClassName      继承javax.microedition.midlet.MIDlet类并实现了无参构造函数的类名

Manifest文件是位于JAR中的/META-INF/MANIFEST.MF(注:将JAR解压后可见)ManifestJAD的语法相同,也可能和JAD有相同的属性。属性位置和查询的规则在技术文档"Retrieving MIDlet Attributes."( http://developers.sun.com/mobility/midp/ttips/getAppProperty/index.html)中描述。ManifestMIDP 2.0 signed-MIDlet model的关键组件。在signed MIDlet suite中,JAD中的属性必须和Manifest中共有的属性值相同。JAD文件属性的修改很容易,不过在认证的MIDlet中除非重新重新认证,否则不能修改。

除了java类文件和Manifest之外,JAR文件还可能包含其他资源,比如MIDlet需要通过javax.microedition.lcdui.Image.createImage(String)方法加载的图片。MIDlet应用也可以调用java.lang.Class.getResourceAsStream(String)方法获取JAR中除类文件之外的所有资源,资源以java.io.InputStream的形式获取。字符串参数为识别JAR中资源的路径。路径的定义和使用规则在JSR 118: Mobile Information Device Profile (MIDP) 2.0 specification36页的"Application Resource Files"部分。

The Security Model(注:http://ajava.org/readbook/J2ME/youxibc/4555.html中有详细解说)

J2ME安全模型是J2SE 模型的微缩版本,一般在J2ME设备内置的资源中已经应用。

规则如下:

类文件在包含进JAR文件之前进行接口符合验证。

受限的API集合:CLDC(联网的受限设备配置),相关概况,许可开放类

应用本地代码的下载和案例由虚拟机完成,没有用户定义的类加载器。

本地的JNI(Java Native Interface)和用户扩展是非法的。

系统类不允许被重载。

简而言之:

MIDlet不能修改运行时环境

MIDlet不能修改自身

MIDlet不能脱离运行时环境

Installation

 

There are two ways to install a MIDlet suite. The first, called direct, involves some direct connection between the device and the development platform – commonly cable, infrared, or Bluetooth link. In the case of the Nokia 6100, for example, it's a Nokia DKU-5 USB cable and the Nokia PC Suite software, which includes Nokia Application Installer. You develop the MIDlet suite, perhaps test it in an emulator, then install it using the USB cable and Nokia Application Installer. While this method is efficient for testing on your own device, it is hardly suitable for deploying an application to millions of end users.

 

Over-the-air provisioning (OTA) makes large-scale deployment possible – even easy. A device can install a MIDlet suite from a remote server using the device's built-in browser. Simply entering the URL of the suite's JAD file into the browser address field starts the installation process. In general terms:

The client device sends an HTTP GET request to the server for the given URL.

The server sends an HTTP response with the suite's JAD file as the message body.

The client verifies the HTTP response and extracts the suite's MIDlet-Jar-File and MIDlet-Jar-Size attributes.

The device sends an HTTP GET request for the JAR file.

The server sends an HTTP response with the JAR file as the message body.

The device verifies the message and the JAR file.

The device asks the user to verify installation.

 

 

This description omits steps relating to signed MIDlet suites, permissions, and push-registry entries, collapsing them into the "verifies message and JAR file" step. Error handling has also been omitted to simplify presentation.

 

The success of the installation process depends on correct functioning of the web server and the device's browser. Characteristics of the network between the device and server can affect installation too. One frequent cause of OTA failure is a size limit that network elements impose on the size of the JAR file. Another is specifying incorrect MIME types for JAD and JAR files. The correct MIME type for a JAD file is text/vnd.sun.j2me.app-descriptor and for a JAR file is application/java-archive.

Removal

 

Here's the easiest part: Because a MIDlet suite is a self-contained entity, deleting it is simple. Most devices allow the user to select a MIDlet suite and choose a Delete option from a menu. At this point the device likely asks for confirmation; a positive response removes the MIDlet suite, including any push-registry entries and record stores created by any MIDlet in the suite.

 

On a Sony Ericsson T616, for instance, the deletion process looks like this:

Press the joystick button to access the application page.

Using the joystick, scroll to the Entertainment icon and select it.

Select the Games & More item from the list.

Select a MIDlet from the list.

Press the More button.

Select the Delete option from the list.

Select Yes in the Delete confirmation dialog. (The MIDlet is deleted.)

Select OK in the Deleted confirmation dialog.

Different devices have different procedures but the idea is the same; in the end the MIDlet suite is gone.

The Quintessential First Program

 

Since the days of Kernighan and Ritchie's The C Programming Language (1978), the first program most developers attempt when they begin using a new language or environment is Hello World. The C version contains only a few lines, including the specification of an I/O library, and the output is only a simple greeting, but this venerable program gives you the chance to prove much: that you can write, build, debug (if necessary), and run a program. So developers using Java technology won't be left out, the article "Wireless Development Tutorial Part I"( http://developers.sun.com/mobility/midp/articles/wtoolkit/) provides a J2ME version of Hello World and instructions on building it.

Execution States

MIDlet开始执行的时候,AMS首先调用无参的构造函数创建一个MIDlet实例。构造函数基本很少做初始化工作,甚至做。AMS框架提供了过渡,我们使用这些过渡来作为资源管理的控制点,很快你会看相关内容。当构造函数返回时,AMSMIDlet置为paused状态。AMS调用startApp()方法将其转换为active状态。

AMS调用pauseApp()时,MIDletactive返回到paused状态,可以把pauseApp()理解为与startApp()相反的方法。paused状态下,MIDlet并没有被终止,不过它会释放所有在startApp()时得到的资源。MIDlet可能在pausedactive状态下转换多次,每次转换在调用startApp()pauseApp()发生。

这些状态过渡方法是我们能有效的管理资源。典型地,使用startApp()分配记录存储、网络连接、UI控件等等,使用pauseApp()释放这些资源。

唯一剩下的一个问题是:one-time resources other resources 的区别是什么?这个问题没有确切的答案,只有一些指导:

one-time resources:一次性资源

UI components           UI控件

Background threads 后台线程

Timers and Tasks       计时器和任务

not one-time resources:非一次性资源

Network connections and IO streams                   网络连接和IO

Resources that can be shared between MIDlets     可以在MIDlet之间共享的资源

资源需求和释放的顺序遵循LIFO的原则(last in first out),和栈操作一样;如果资源需求的顺序是A-B-C,那么释放的顺序是C-B-A

在调用destroyApp()的时候,MIDletactive或者paused状态转换到destroyed状态,这个方法将释放所有构造函数以及保存状态信息需要的资源。

Paused/Active/Destroyed状态过渡的话题有一些不同。首先,MIDlet将通过调用notifyPaused()自行进入paused状态。相似的,通过调用notifyDestroyed()通知AMS可以将此MIDlet销毁了。destroyApp()其实要传入一个boolean参数,如果传入trueMIDlet将在destroyApp()返回时进入Destroyed状态;但是如果传入falseMIDlet通过抛出MIDletStateChangeException异常来拒绝进入Destroyed状态。请谨记着只是向AMS送达的一个请求。最后,resumeRequest()方法的调用将通知AMS MIDlet想要进入active状态。或许你会问,一个处于paused状态的MIDlet如何调用resumeRequest()?虽然处于paused状态的MIDlet大部分情况下是停止的、空闲的,它可以处理一步事件,类似于计时器(timer)和回调函数(callback)

 还有一些剩下的问题,关于AMSMIDlet互相通信的,可以用whywhen来概括。例如,startApp()何时被调用,pauseApp()又为什么被调用?规范故意模糊了这些领域,允许实现遵守这些规则相反又允许主机运行时环境变化规则。例如:假设一部级检测到了一条新收到的彩信,AMS将停止正在运行的MIDlet以释放彩信所需的内存。如果挺止应用没有释放足够的内存,AMS将调用destroyApp()释放更多的内存。这类问题答案的不确定性或许不是开发者们需要的,但是这是规范制定者能给出的最终答案。

Sample Source

 

Source code available from the Java mobility site comes in two forms: one for online viewing and another for download. The download version is in J2ME Wireless Toolkit application format. The following table also supplies a reference to the article relevant to each sample. If you're using the J2ME Wireless Toolkit, you can begin working with the source files simply by unzipping the archive and moving the applications into the apps subdirectory of your toolkit installation. Article      Online       Download

Retrieving MIDlet Attributes    

http://developers.sun.com/mobility/midp/ttips/getAppProperty/index.htm         

MyMidlet.java     attributes.zip

Managing the MIDlet Life-Cycle with a Finite State Machine     

http://developers.sun.com/mobility/midp/articles/fsm/

FsmMIDlet.java

FSM.java    fsm.zip

Wireless Application Programming: MIDP Programming and Packaging Basics

http://developers.sun.com/mobility/midp/articles/getstart/

FirstMIDlet.java

HelloMIDlet.java       getstart.zip

Wireless Development Tutorial, Part I    

http://developers.sun.com/mobility/midp/articles/wtoolkit/

HelloMIDlet.java wtoolkit.zip

 

Which brings us to the toolkit itself – the J2ME Wireless Toolkit, of course, a state-of-the-art toolbox for developing wireless applications based on MIDP. The current release at this writing, 2.2, supports CLDC 1.0 and 1.1, MIDP 1.0 and 2.0, and seven other J2ME JSRs. The toolkit allows the developer to choose the JSRs that best match the target environment, build JAD and JAR files, and run MIDlets in an emulator before deploying them on target devices. The toolkit is available for download.

Summary

 

This learning path has described the basics of the MIDlet runtime environment, the AMS, including its security model, available APIs, and execution-state machine. Several sample MIDlets, with related articles, are available in both online and download formats for your further education. Future learning paths will cover such topics as network and resource I/O, user interface, persistent storage (RMS), and a collection of miscellaneous subjects.