MIDP2.0手机上的手电筒程序(附代码)

MIDP2.0手机手电筒程序及代码
本文介绍了可在支持MIDP2.0版本手机上运行的手电筒程序。该程序利用Display对象控制手机背景,只能实现闪烁背景灯功能。文中还给出了LightMIDlet.java和LightForm.java两个文件的实现代码。

MIDP2.0手机上的手电筒程序(附代码)

作者:陈跃峰

出自:http://blog.youkuaiyun.com/mailbomb

可以在所有支持MIDP2.0版本的手机上运行的手电筒程序。程序实现的原理是:利用Display对象来实现对于手机背景等的控制,在该系列的手机中只能实现闪烁背景灯的功能。实现代码如下:

//文件名:LightMIDlet.java

package light;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class LightMIDlet extends MIDlet {
static LightMIDlet instance;
LightForm displayable = new LightForm(this);
public LightMIDlet() {
instance = this;
}

public void startApp() {
Display.getDisplay(this).setCurrent(displayable);
}

public void pauseApp() {
}

public void destroyApp(boolean unconditional) {
}

public static void quitApp() {
instance.destroyApp(true);
instance.notifyDestroyed();
instance = null;
}

}

//文件名:LightForm.java

package light;

import javax.microedition.lcdui.*;

public class LightForm extends Form implements CommandListener {
StringItem si;
Command cmdOpen;
Command cmdClose;
Command cmdExit;
Display display;
public LightForm(LightMIDlet lm) {
super("手电筒");
si = new StringItem("手电筒状态:","打开");
this.append(si);

display = Display.getDisplay(lm);

cmdOpen = new Command("打开",Command.OK,1);
this.addCommand(cmdOpen);

cmdClose = new Command("关闭",Command.CANCEL,1);
this.addCommand(cmdClose);

cmdExit = new Command("退出",Command.EXIT,1);
this.addCommand(cmdExit);

setCommandListener(this);

boolean b;

b = display.flashBacklight(1000000);

if(b == false){
Alert alert = new Alert("手电筒","您的手机不支持该功能!",null,AlertType.INFO);
alert.setTimeout(3000);
display.setCurrent(alert);
}
}

public void commandAction(Command c, Displayable d) {
//关闭
if (c == cmdExit) {
// stop the MIDlet
LightMIDlet.quitApp();
}
//打开
if(c == cmdOpen){
display.flashBacklight(1000000);
si.setText("打开");
}

if(c == cmdClose){
display.flashBacklight(0);
si.setText("关闭");
}
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值