资源描述
嵌入式软件开发
Java ME实验指引
周景 杨凤年 编
长沙学院计算机科学与技术系
前 言
进入21世纪,嵌入式领域发展令人刮目相看,各种各样令人眼花缭乱嵌入式设备层出不穷,例如智能手机、PDA、MP3\4、机顶盒以及智能家电,这些嵌入式设备硬件性能日益强大,甚至足以与PC机相媲美,但与桌面应用领域相比,嵌入式领域应用软件开发始终是一种软肋,无论是数量上还是质量上,嵌入式应用软件与桌面应用软件均有非常大差距,同步这也意味着嵌入式应用软件有着辽阔空间。
嵌入式平台软件开发面临最大问题一是如何充分运用有限资源,二是应用程序兼容性,第一种问题随着嵌入式微解决迅速发展以及存储技术不断改进得到了很大缓和,然而第二个问题依然是一种很大困扰,不同嵌入式设备之间差别会非常大,应用程序要想获得不同平台兼容性是一种非常大挑战,Java ME跨平台特性对于解决嵌入式软件开发平台兼容性有非常大协助,同步Java ME工具也非常完善并且简朴易用,诸多嵌入式嵌入式设备和操作系统都提供了对于Java支持,因从技术看,使用Java ME开发嵌入式软件是一种非常好选取。
从市场方面分析,嵌入式软件开发特别是手机应用软件有无与伦比美好将来,苹果公司自发布iphone手机,截至11月,iphone上应用程序数量达到了10万个,而通过苹果应用程序商店付费下载下载次数达到20亿次,总共销售额超过了15亿美金,手机应用软件市场合蕴含商业价值毋庸置疑,这也将成为推动手机软件开发和技术创新强大动力。
为了协助学习Java ME软件开发和制作水平,特编制该实验指引书,该实验指引书重要参照了Java ME API docs和《J2ME手机游戏开发技术详解》一书,将严格遵循由浅入深、循序渐进原则。因编者经验、水平和写作时间限制,书中难免有疏漏、不当和错误之处,欢迎读者批评指正。祝学习高兴!
编者
3月
实验一 高档顾客界面设计
1.实验目
(1) 建立Java ME开发环境
(2) 运用MIDP高档屏幕对象设计顾客界面
2.实验类型:验证型
3.实验学时:4
4.实验原理和知识点
(1) 实验原理:熟悉NetBean开发环境,学习MIDP关于高档屏幕对象使用,学习MIDP事件机制
(2) 知识点: NetBean,高档屏幕对象,事件机制
5.实验环境(硬件环境、软件环境):
(1) 硬件环境:Intel Pentium III 以上CPU,1GB以上内存
(2) 软件环境:JDK6,Java ME SDK 3.0,NetBean 6.8
6.实验指引:
(1) 建立Java ME开发环境
下载并按顺序安装如下软件(附软件下载地址)
JDK SE 6()
Java Me SDK 3.0()
Netbean IDE 6.8 for Java()
(2) 创立第一种Java ME应用程序HelloWorld
打开NetBean,使用菜单命令“文献”->“新建项目”,弹出窗口,在左边类别选取“Java ME”,在右边项目“Mobile 应用程序”,点击“下一步”(见下图)
输入项目名称,选取项目位置和项目文献夹,点击“下一步” ,注意不要选取“创立Hello MIDlet”选项(见下图)
选取仿真器平台和设备,点击下一步
弹出窗口,选取“完毕”。 项目创立成功。
右键点击项目,在弹出菜单中选取命令“新建”->“MIDlet..”
在弹出窗口中,为MIDlet类和包命名,点击按钮“完毕”。
NetBean生成文献HelloWorld.java,文献详细内容如下:
在代码模板中添加如下代码
编译并运营程序,运营效果如下所示
(3) Display对象使用,创立并添加Command对象,设立事件监听器使得顾客界面可以与顾客交互
Display类是系统显示屏幕和输入设备管理器,用来获取设备属性办法以及用来获取要显示屏幕对象, 通过如下代码获取Display对象实例:
Display display=Display.getDisplay(midlet);
Displayable是可以显示在屏幕上对象组件公共父类。在设备屏幕上,同一时刻应用程序最多只有一种Displayable对象显示在屏幕上,通过它可以和顾客进行交互。通过。通过Display对象setCurrent()办法 可以设立当前显示对象。例如:
display.setCurrent(textBox);
这里textBox是一种Displayable子类子类TextBox一种实例。
Java ME通过事件机制来解决顾客交互,其中高档事件解决核心代码如下所示:
} 初始化COMMAND对象实例:
◦ EXIT_CMD = new Command(“Exit”,Command,1);
} 将Command对象添加进Displayable对象
◦ textBox.addCommand(EXIT_CMD);
} 为Displayable对象绑定事件监视器
◦ textBox.addCommandListener(this);
} 实现commandAction办法,添加触发事件时需要进行动作
◦ public void commandAction(Command c,Displayable s) {
◦ if(c==EXIT_CMD) {
◦ destroyApp(false);
◦ notifyDestroyed();
◦ }
◦ }
使用Command对象来响应顾客按键事件基本环节如下:
1. 定义软键(new Command)
2. 将其注册到容器对象中,例如:Form、Alert、List、TextBox(调用addCommand办法)
3. 设定CommandListener(调用setCommandListener办法),实现CommandAction办法来响应顾客按下软键命令应当实现行为
下面是一种程序示例,演示了如何添加Command对象到屏幕对象,顾客可以使用这些命令在各种屏幕对象之间进行跳转。
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class ShowScreen extends MIDlet implements CommandListener{
private Display display;
private Form props;
private Command backCommand = new Command("BACK",Command.BACK,2);
private Command cancelCommand = new Command("CANCEL",Command.CANCEL,1);
private Command exitCommand = new Command("EXIT",Command.EXIT,1);
private Command helpCommand = new Command("HELP",Command.HELP,1);
private Command itemCommand = new Command("ITEM",Command.ITEM,1);
private Command okCommand = new Command("OK",Command.OK,1);
private Command screenCommand = new Command("SCREEN",Command.SCREEN,1);
private Command stopCommand = new Command("STOP",Command.STOP,1);
public ShowScreen(){
display = Display.getDisplay(this);
}
public void startApp() {
props = new Form("Hello World");
props.append("Hello World!\n");
props.addCommand(backCommand);
props.addCommand(cancelCommand);
props.addCommand(exitCommand);
props.addCommand(helpCommand);
props.addCommand(itemCommand);
props.addCommand(okCommand);
props.addCommand(screenCommand);
props.addCommand(stopCommand);
props.setCommandListener(this);
display.setCurrent(props);
}
public void showScreen(String cmd) {
Form form=new Form("show cmd");
form.append(cmd);
form.addCommand(exitCommand);
form.setCommandListener(this);
display.setCurrent(form);
}
public void commandAction(Command c,Displayable s){
if (c == exitCommand)
{
destroyApp(false);
notifyDestroyed();
}
else if(c==helpCommand)
{
showScreen("help");
}
else if(c==backCommand)
{
showScreen("back");
}
else if(c==cancelCommand)
{
showScreen("cancel");
}
else if(c==itemCommand)
{
showScreen("item");
}
else if(c==okCommand)
{
showScreen("ok");
}
else if(c==screenCommand)
{
showScreen("screen");
}
if(c==stopCommand)
{
showScreen("stop");
}
}
public void destroyApp(boolean unconditional)
{
}
public void pauseApp()
{
display.setCurrent(null);
props = null;
}
}
(4)高档屏幕对象Alert使用
Screen类是高档屏幕对象公共父类,Screen类有4个子类:Alert,List,TextBox和Form。
Alert类用来在设备上显示一种告警屏幕并显示一种文本字符串,还可以涉及图像和声音。Alert类有两个构造函数:
第一种只指定标题
◦ Alert alert = new Alert(“Hello”);
◦ alert.setImage(icon);
◦ alert.setString(“this is a alert”);
◦ alert.setTimeout(5000);
◦ alert.setType(AlertType.ERROR);
第二个指定标题、内容、图像和警告类型
◦ Alert alert = new Alert(“Hello”,”this is a alert”,icon,AlertType.ERROR);
可以运用addCommand()办法为Alert对象添加软键,如果Alert自身没有加入任何Command,它静态成员DISMISS_COMMAND会作为唯一系统菜单项。一旦加入自定义Command,DISMISS_COMMAND就不会浮现。用removeCommand()删除所有Alert中Command后,DISMISS_COMMAND会再次浮现,保证Alert上至少有一种选项可用。
AlertType提供了几种定义好类型来辅助Alert类使用,这些类型涉及:
l ALARM:向顾客警告,这是顾客预先祈求告知
l COMFIRMATION:用来确认一种顾客动作,例如:“”数据已经成功保存
l ERROR:向顾客提示一种错误操作,例如:“空间局限性,无法保存数据”
l INFO:普通向顾客提供非危险性信息
l WARNING:向顾客警告一种危险操作,例如:“该操作将删除所有数据”
AlertType类还可以用来播放警告声音,示例代码如下:
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
public class AlertTypeDemo
extends MIDlet implements CommandListener{
private Command CMD_EXIT=new Command("Exit",Command.EXIT,1);
private Command CMD_INFO=new Command("INFO",Command.SCREEN,2);
private Command CMD_WARN=new Command("WARN",Command.SCREEN,3);
private Command CMD_ERROR=new Command("ERROR",Command.SCREEN,4);
private Command CMD_ALARM=new Command("ALARM",Command.SCREEN,5);
private Command CMD_CONF=
new Command("CONFIRMATION",Command.SCREEN,6);
private Display display;
private Form mainForm;
private AlertType alertType;
public AlertTypeDemo() {
display = Display.getDisplay(this);
alertType = AlertType.ALARM;
mainForm = new Form("AlertType演示播放声音");
mainForm.addCommand(CMD_EXIT);
mainForm.addCommand(CMD_INFO);
mainForm.addCommand(CMD_WARN);
mainForm.addCommand(CMD_ERROR);
mainForm.addCommand(CMD_ALARM);
mainForm.addCommand(CMD_CONF);
mainForm.setCommandListener(this);
}
protected void startApp() {
display.setCurrent(mainForm);
}
protected void destroyApp(boolean unconditional) {
}
protected void pauseApp() {
}
public void commandAction(Command c,Displayable s) {
int priority = c.getPriority();
switch(priority){
case 2:alertType = AlertType.INFO;break;
case 3:alertType = AlertType.WARNING;break;
case 4:alertType = AlertType.ERROR;break;
case 5:alertType = AlertType.ALARM;break;
case 6: alertType = AlertType.CONFIRMATION;break;
}
if(alertType.playSound(display)){
mainForm.append("声音已经播放\n");
}
if (c == CMD_EXIT){
destroyApp(false);
notifyDestroyed();
}
}
}
(5)Choice接口与List类使用
Choice提供了从预定义选项中进行选取功能,类List和ChoiceGroup实现了这个接口。
Choice对象分为4类:
l EXCLUSIVE:顾客只能选取一种选项
l IMPLICIT:当软键命令激活时,顾客当前焦点选项会被选中(只用于List对象)
l MULTIPLE:多重选取
l POPUP:弹出选项框,只能选取一种(只用于ChoiceGroup对象)
Choice接口每个选项均有一种文本字符串和一种可选图像,如果不涉及图形,可以设立参数imagePart为null。 所有图像尺寸应当相似,图像尺寸不能过大 选项内容过长而不能较好显示,实现将为顾客提供整个选项可见办法,例如折成多行显示
Choice对象生成之后,选项可以被动态插入(insert)、追加(append)或者删除(delete)。Choice各个选项均有它们索引值引用,这些索引都是持续整数,值从0到size()-1。size()返回Choice选项对象中选项个数
List类是一种包具有选取列表Screen子类,它大多数行为都和类ChoiceGroup以及在Choice接口定义通用API一致。List只有3种类型:EXCLUSIVE、IMPLICIT、MULTIPLE。
当顾客从IMPLICIT模式List中选取一种选项时,列表会使用一种特殊Command对象告知CommandListener,这个特殊Command对象就是SELECT_COMMAND
下面代码演示了List类是如何工作。(注意如果要想显示选项中图片,必要要在项目中添加图片资源所在文献夹)
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
public class ListDemo
extends MIDlet
implements CommandListener {
private final static Command CMD_EXIT =
new Command("Exit",Command.EXIT,1);
private final static Command CMD_BACK =
new Command("Back",Command.BACK,1);
private Display display;
private List mainList;
private List exclusiveList;
private List implicitList;
private List multipleList;
private boolean firstTime;
public ListDemo() {
display = Display.getDisplay(this);
System.out.println(display.getBestImageHeight(Display.LIST_ELEMENT));
System.out.println(display.getBestImageWidth(Display.LIST_ELEMENT));
// these are the strings for the choices.
String[] stringArray = {
"Option A",
"Option B",
"Option C",
"Option D"
};
// the string elements will have no images
Image[] imageArray = null;
try {
// load the duke image to place in the image array
Image img = Image.createImage("/option.png");
// these are the images and strings for the choices.
imageArray = new Image[] {
img,
img,
img,
img
};
} catch (java.io.IOException err) {
// ignore the image loading failure the application can recover.
}
/*
* create samples of the supported types:
* "Exclusive","Implicit" and "Multiple"
*/
exclusiveList = new List("Exclusive",Choice.EXCLUSIVE,stringArray,imageArray);
exclusiveList.addCommand(CMD_BACK);
exclusiveList.addCommand(CMD_EXIT);
exclusiveList.setCommandListener(this);
implicitList = new List("Implicit",Choice.IMPLICIT,stringArray,imageArray);
implicitList.addCommand(CMD_BACK);
implicitList.addCommand(CMD_EXIT);
implicitList.setCommandListener(this);
multipleList = new List("Multiple",Choice.MULTIPLE,stringArray,imageArray);
multipleList.addCommand(CMD_BACK);
multipleList.addCommand(CMD_EXIT);
multipleList.setCommandListener(this);
firstTime = true;
}
protected void startApp() {
if (firstTime) {
// these are the images and strings for the choices.
Image[] imageArray = null;
try {
// load the duke image to place in the image array
Image icon = Image.createImage("/Icon.png");
// these are the images and strings for the choices.
imageArray = new Image[] {
icon,
icon,
icon
};
} catch (java.io.IOException err) {
// ignore the image loading failure the application can recover.
}
String[] stringArray = {
"Exclusive",
"Implicit",
"Multiple"
};
mainList = new List("Choose type",Choice.IMPLICIT,stringArray,imageArray);
mainList.addCommand(CMD_EXIT);
mainList.setCommandListener(this);
display.setCurrent(mainList);
firstTime = false;
}
}
protected void destroyApp(boolean unconditional) {
}
protected void pauseApp() {
}
public void commandAction(Command c,Displayable d) {
if (d.equals(mainList)) {
// in the main list
if (c == List.SELECT_COMMAND) {
if (d.equals(mainList)) {
switch (((List)d).getSelectedIndex()) {
case 0:
display.setCurrent(exclusiveList);
break;
case 1:
display.setCurrent(implicitList);
break;
case 2:
display.setCurrent(multipleList);
break;
}
}
}
} else {
// in one of the sub-lists
if (c == CMD_BACK) {
display.setCurrent(mainList);
}
}
if (c == CMD_EXIT) {
destroyApp(false);
notifyDestroyed();
}
}
}
(6) TextBox类使用
TextBox类继承了Screen类,容许顾客输入并且编辑文本。TextBox类构造函数如下所示:
TextBox(String title,String text,int maxSize,int constraints)
有4个参数:
title是要在屏幕上显示标题
l text是初始化内容,可以是null
l maxSize是TextBox对象所能容纳最大字符数,是不不大于0整数
l constraints为输入约束,例如可以限制只能输入数字0~9等,关于输入约束请参照TextField类
TextBox和TextField类具备同样输入约束概念,办法setConstraints和getConstraints分别用来设立和获取TextBox输入约束
约束参数使用是TextField类中定义常数:
l ANY:可以输入任意文本
l EMAILADDR:只能容许输入EMAIL地址
l NUMERIC:只容许输入整数
l PHONENUMBER:只容许输入电话号码
l URL:只容许输入url地址
l DECIMAL:容许输入带小数点数字
一下代码演示了TextBox类是如何工作。
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
public class TextBoxDemo
extends MIDlet
implements CommandListener {
private Display display;
private ChoiceGroup types;
private ChoiceGroup options;
private Form mainForm;
private final static Command CMD_EXIT = new Command("Exit",Command.EXIT,1);
private final static Command CMD_BACK = new Command("Back",Command.BACK,1);
private final static Command CMD_SHOW = new Command("Show",Command.SCREEN,1);
s
展开阅读全文