分享
 
 
 

Introduction to Wireless Programming with the MID Profile

王朝java/jsp·作者佚名  2006-01-08
窄屏简体版  字體: |||超大  

MIDP无线编程先导

莫妮卡.帕瓦兰

1.0版

2000年9月

J2ME移动设备配置文件(MIDP)< Mobile Information Device Profile>提供了编写无线应用面向Java API的一个平台,使任何手机和双路呼机都支持MIDP。要使用MIDP的API,你必须先下载并安装J2ME的 有限连接设备配置(CLDC)<Connected Limited Device Configuration>。

MIDP应用程序的另一个名字是MIDlet.向applet一样,MIDlet也必须被软件调用才能运行. In the case of an applet, the underlying software is a browser or the appletviewer tool. In the case of a MIDlet, the underlying software is the cell phone or two-way pager device implementation that supports the CLDC and MIDP.

This article is the first in a series on MIDP application programming. It introduces you to the MIDlet test environment and the MIDP APIs with a very simple MIDlet example.

MIDLet的一个例子介绍

MIDlet测试环境

HelloMIDlet用户界面

HelloMIDlet代码

类申明

Instance Variables

Constructor

Lifecycle Methods

Event Handling

Conclusion

MIDlet一个例子介绍

The MIDP download includes an examples directory that contains a number of example MIDlets. One of those examples is HelloMIDlet. The only thing it does is display text in the display area of a mobile phone or two-way pager. While the code is very short and simple, this MIDlet is a good place to start learning the MID profile because it introduces you to the MIDlet test environment and the structure of a MIDlet.

Note: Instructions for compiling and running the HelloMIDlet example are in MIDP Setup Explained. You can find additional information in the MIDP download under docs/MIDletInstructions.html.

MIDlet测试环境

The figure at right shows how HelloMIDlet looks when it runs. The MIDP executable is a test environment that provides the cell phone image, display area, and key input. All MIDlets extend the MIDlet class, which is the interface between the runtime environment (the application manager), and the MIDlet application code. The MIDlet class provides programmatic interfaces for invoking, pausing, restarting, and terminating the MIDlet application. MIDP applications are portable across cell phones and pagers, and the MIDP platform handles differences in screen size and button layout.

The MIDlet runs in the display area and accepts keypad input by way of mouse clicks. There are several kinds of keys on the MIDlet test environment keypad: soft buttons, control keys, navigation keys, and alphanumeric keys.

The soft buttons are the two angled keys to the left and right of and directly below the display area. Soft buttons invoke application commands, and their exact functionality is determined by the labels that appear near them on the screen. In the figure at right, the left soft button is mapped to the Exit command shown in the display just above it.

The control keys are the keys with the green and red telephone handset icons on them. Control keys are found on most hand-held phones and sometimes labeled Talk and End. Because the test environment is not an actual phone, control keys are not used for any call control from within the MIDlet; however, the End (red) key terminates the currently running MIDP application.

While the red key terminates the running MIDlet by calling the destroyApp(true) method, invoking the Exit command by pressing the Exit soft key sends a command event to the application's commandAction method, which you can implement to exit or doing something else.

The navigation keys are the five keys with the arrows and dot. Navigation keys let you move around and select items in the display area.

The alphanumeric keys are the 12 keys with numbers, letters, and symbols on them at the bottom. Alphanumeric keys let you enter numbers, letters, and symbols into the display area.

When a text entry screen is showing, the button on the alphanumeric keypad with the pound sign and up arrow lets you cycle through several different input modes that allow entry of lowercase letters, uppercase letters, numbers, or symbols. The top of the display area indicates which one is active.

Letters Active: With uppercase or lowercase letters active, you cycle through the three letters on each button by repeatedly clicking the button until the letter you want on that button appears.

Symbols Active: With symbols active, the symbols appear in the display area and you use the navigation keys to locate the one you want. Click the center navigation key with the dot on it to select a symbol.

Erasing: The key with left arrow lets you backspace over numbers, letters, or symbols.

HelloMIDlet用户界面

The HelloMIDlet user interface is created from a TextBox component, which is similar to the Abstract Window Toolkit (AWT) TextArea and TextField components. MIDlet UI components are designed to meet the low memory and small footprint requirements of cell phone and two-way pager display areas. If you are already familiar with creating user interfaces using AWT components, your learning curve for creating MIDlet user interfaces will be somewhat shorter because you can apply some of what you already know to MIDlet components.

MIDlet TextBox Component

A MIDlet TextBox component has three areas: title, initial contents, and command. When you create the TextBox component, you specify text for the title and initial contents areas. Once created, you add commands to its command area and tell the MIDlet to listen for action events by calling the TextBox.setCommand and TextBox.setCommandListener methods.

In the HelloMIDlet example, the Hello MIDLet text appears in the title area, and the Test String text appears in the initial contents area. The Exit command appears in the command area, and is mapped to the control keys directly below it and to the right.

Mapping Commands to Keys

The command-to-key mapping is device dependent, and handled by the device according to the type specified when you create the command. The MIDP Command class lets you specify the following command types:

BACK, CANCEL, EXIT, HELP, ITEM, OK, SCREEN, and STOP.

In the HelloMIDlet example, pressing the soft button that has the Exit label above it calls the MIDlet's commandAction() method and passes it a reference to the exitCommand command. Pressing the red key calls the MIDlet's destroyApp(true) method. After the MIDlet exits by either invocation, the test environment returns to the application manager if it was invoked by the application manager, or exits the test environment entirely.

Priority Value

If an application has more than one command of the same type, the command's priority value describes its importance in terms of the command to key mappings. The lower the priority value, the greater the importance of the command.

The device chooses the placement of a command based on the type of command and then places commands with the same type by their priority order. This might mean that the command with the highest priority is placed so the user can trigger it directly, and that commands with a lower priority are placed on a menu.

HelloMIDlet代码

MIDlet code structure is very similar to applet code. There is no main method and MIDlets always extend from the MIDLet class. The user interface components are in the lcdui package, and the MIDlet class in the MIDlet package provides methods to manage a MIDlet's life cycle.

The complete, uncommented code appears here. The sections below describe the parts of the MIDlet code beginning with the class declaration.

import javax.microedition.midlet.*;

import javax.microedition.lcdui.*;

public class HelloMIDlet extends MIDlet

implements CommandListener {

private Command exitCommand;

private Display display;

private TextBox t = null;

public HelloMIDlet() {

display = Display.getDisplay(this);

exitCommand = new Command("Exit", Command.EXIT, 2);

t = new TextBox("Hello MIDlet",

"Test string",

256, 0);

t.addCommand(exitCommand);

t.setCommandListener(this);

}

public void startApp() {

display.setCurrent(t);

}

public void pauseApp() {

}

public void destroyApp(boolean unconditional) {

}

public void commandAction(Command c, Displayable s) {

if (c == exitCommand) {

destroyApp(false);

notifyDestroyed();

}

}

}

类申明

The HelloMIDlet class extends MIDLet and implements the CommandListener interface. The MIDlet class defines an MIDP application and provides the life cycle methods. The CommandListener interface allows a HelloMIDlet object to be an action listener for command events. In this example, the HelloMIDlet object listens for exit events.

public class HelloMIDlet extends MIDlet

implements CommandListener {

Instance Variables

Instance variables store the unique data for an instance of the HelloMIDlet class, and make the data available to the constructor and life cycle methods. The Command, Display, and TextBox instance variables are initialized in the constructor.

private Command exitCommand;

private Display display;

private TextBox = null;

Constructor

The constructor gets the Display object for this instance of the HelloMIDlet class and creates a command of type EXIT with a priority of 2. The Display object represents the manager of the display and input devices of the system. It includes methods for retrieving properties of the device and for requesting that objects be displayed on the device.

The constructor creates and initializes the MIDlet's user interface, which is a TextBox object with a title of Hello MIDlet, display text of Text string, a maximum character width of 256 characters, and no constraints on inputs. A TextBox receives inputs from the keypad, and input constraints restrict the characters that can be entered. Input constraints do not currently impose any syntax checking or formatting.

The startApp method does the following to set up command action listening and make the user interface visible:

The exitCommand is added to the TextBox.

The HelloMIDlet action listener is set on the TextBox (t.setCommandListener(this)) so the HelloMIDlet.commandAction method will be called whenever a command on the TextBox generates an event.

The TextBox is passed to the Display to make it visible on the device's display. Every MIDlet has one and only one Display object. public HelloMIDlet() {

display = Display.getDisplay(this);

exitCommand = new Command("Exit", Command.EXIT, 2);

t = new TextBox("Hello MIDlet",

"Test string",

256, 0);

t.addCommand(exitCommand);

t.setCommandListener(this);

}

Lifecycle Methods

The underlying device implementation controls the MIDlet by calling its life cycle methods. The device implementation calls the startApp method to make the MIDlet ready for use. In this example, the startApp() method makes the Display current.

public void startApp(){

display.setCurrent(t)

}

public void pauseApp() { }

public void destroyApp(boolean unconditional) { }

The device implementation calls the pauseApp method to stop the MIDlet temporarily. This method should release shared resources such as threads or connections. This simple example has no shared resources. The implementation might ask the MIDlet to continue by calling the startApp() method again.

The device implementation calls the destroyApp method to terminate and destroy the MIDlet. This method should release all shared and local resources, and save any persistent data such as preferences or state. This simple example uses no resources or persistent data.

Event Handling

The commandAction method is passed an event object that represents the action event that occurred. Next, it uses an if statement to find out which component had the event, and takes action according to its findings.

If the command action is a command of type Exit, the application is destroyed, and the application management software is notified that the MIDlet has entered the destroyed state (notifyDestroyed).

public void commandAction(Command c, Displayable s) {

if (c == exitCommand) {

destroyApp(false);

notifyDestroyed();

}

}

}

Conclusion

This simple example shows how easy it is to write and run a small MIDlet consisting of one user interface component and command. MIDlet programming is easy in comparison to programming with Java 2, Standard Edition, because the MIDP API is simpler. There are only a handful of classes you need to learn before you can start writing MIDlet applications.

Future articles in this series will cover creating more than one command, placing commands on menus, using a canvas for a drawing area, and making Internet connections.

关于作者: 莫尼卡.帕瓦兰,Java 开发者联盟(JDC)<Java Developer Connection>的专职作家,著书有:《Java编程语言本质手册》(Essentials of the Java Programming Language: A Hands-On Guide)(Addison-Wesley, 2000年), 参与编撰《Java 2 高级编程》(Advanced Programming for the Java 2 Platform )(Addison-Wesley, 2000年).

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有