在J2ME程序加入暂停功能——附实现源代码
作者:陈跃峰
出自:http://blog.csdn.net/mailbomb
在手机程序运行过程中,因为手机来电等事件,需要暂停手机程序的执行,下面就简单的介绍一下该功能的程序实现。
实现该功能需要了解MIDlet的生命周期,在手机来电时,系统会自动调用MIDlet子类中的pauseApp方法,在电话结束时会自动调用startApp重新启动程序,所以我们只需要将处理的代码放在这两个方法内部就可以。
而Nokia在实现时没有严格按照MIDlet的生命周期来实现,所以Nokia系列的手机在实现暂停功能的时候,需要在Canva类中覆盖hideNotify和showNotify方法,其中hideNotify会自动在Canvas不可见的时候被系统调用,showNotify会自动在Canvas可见时被系统调用。
实现暂停功能的原理就是在手机来电的时候停止线程,在电话结束的时候继续开始线程。
下面是一个实现的代码,该代码主界面中实现的是一个不断变大的数字,可以使用在手机来电的时候会自动暂停。该代码在Nokia 6020手机上测试通过。程序中包含三个源文件:PauseMIDlet是MIDlet类文件,MenuList是项目中的游戏功能选择界面,PauseCanvas是程序主界面。下面是程序的源代码:
// 文件名:PauseMIDlet.java
package pausegame;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class PauseMIDlet extends MIDlet {
static PauseMIDlet instance;
MenuList displayable = new MenuList(this);
public PauseMIDlet() {
instance = this;
}
public void startApp() {
//获得当前显示的界面对象
Displayable current = Display.getDisplay(this).getCurrent();
//如果当前界面对象不为空且为PauseCanvas类型,即为暂停动作
if(current != null && current instanceof PauseCanvas){
//启动游戏
((PauseCanvas)current).start();
}else{
//显示游戏界面
Display.getDisplay(this).setCurrent(displayable);
}
}
public void pauseApp() {
Displayable current = Display.getDisplay(this).getCurrent();
if(current != null && current instanceof PauseCanvas){
//暂停游戏
((PauseCanvas)current).pause();
}
}
public void destroyApp(boolean unconditional) {
}
public static void quitApp() {
instance.destroyApp(true);
instance.notifyDestroyed();
instance = null;
}
}
//文件名:MenuList.java
package pausegame;
import javax.microedition.lcdui.*;
public class MenuList extends List implements CommandListener {
private final PauseMIDlet midlet;
private Command cmdOK;
public MenuList(PauseMIDlet midlet) {
super("主菜单", List.IMPLICIT);
this.midlet = midlet;
append("新游戏",null);
append("退出",null);
cmdOK = new Command("确定",Command.OK,1);
addCommand(cmdOK);
setCommandListener(this);
}
public void commandAction(Command c, Displayable d) {
if (c == cmdOK) {
//获得用户的选择
int index = this.getSelectedIndex();
//根据选择显示不同的界面
if(index == 0){
PauseCanvas pc = new PauseCanvas();
Display.getDisplay(midlet).setCurrent(pc);
pc.start();
}else{
PauseMIDlet.quitApp();
}
}
}
}
//文件名:PauseCanvas.java
package pausegame;
import javax.microedition.lcdui.*;
public class PauseCanvas extends Canvas implements Runnable{
private Thread thread;
private int i = 0;
private int width;
private int height;
public PauseCanvas() {
width = this.getWidth();
height = this.getHeight();
}
protected void paint(Graphics g) {
g.setColor(255,0,0);
g.fillRect(0,0,width,height);
g.setColor(0,0,0);
g.drawString(String.valueOf(i),30,30,Graphics.LEFT | Graphics.TOP);
}
public void start(){
thread = new Thread(this);
thread.start();
}
public void pause(){
thread = null;
}
public void showNotify(){
if(thread == null){
//启动程序
start();
}
}
public void hideNotify(){
//暂停程序
pause();
}
public void run(){
Thread current = Thread.currentThread();
while(current == thread){
try{
Thread.sleep(200);
}catch(Exception e){
}
i++;
repaint();
}
}
}