📄 socketdemo.java
字号:
package example.demowireless.socket;
import javax.microedition.lcdui.Choice;
import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
/**
* <p>J2ME Socket Demo</p>
*
* @author 刘光辉
* @version 2009.02.04
*/
public class SocketDemo extends MIDlet implements CommandListener {
private static final String SERVER = "Server";
private static final String CLIENT = "Client";
private static final String[] names = {SERVER,CLIENT};
private Display display;
private Form form;
private ChoiceGroup cg;
private boolean isPaused;
private Server server;
private Client client;
private Command CMD_EXIT = new Command("Exit", Command.EXIT, 1);
private Command CMD_START = new Command("Start", Command.ITEM, 1);
/**
* <p>SocketDemo的构造函数</p>
*/
public SocketDemo() {
display = Display.getDisplay(this);
form = new Form("Socket Demo");
cg = new ChoiceGroup("Please select peer", Choice.EXCLUSIVE, names,
null);
form.append(cg);
form.addCommand(CMD_EXIT);
form.addCommand(CMD_START);
form.setCommandListener(this);
display.setCurrent(form);
}
public boolean isPaused() {
return isPaused;
}
/**
* <p>启动程序,使MIDlet处于active状态</p>
* <p></p>
* @see javax.microedition.midlet.MIDlet#startApp()
*/
protected void startApp() throws MIDletStateChangeException {
isPaused = false;
}
/**
* <p>暂停程序,使MIDlet处于pause状态</p>
* @see javax.microedition.midlet.MIDlet#pauseApp()
*/
protected void pauseApp() {
isPaused = true;
}
/**
* <p>销毁程序,使MIDlet处于destroyed状态</p>
*
* @param boolean unconditional
* <ul>true 释放资源保存数据进入destroyed状态,false 抛出异常保持当前状态</ul>
* @see javax.microedition.midlet.MIDlet#destroyApp(boolean)
*/
protected void destroyApp(boolean unconditional) throws MIDletStateChangeException {
if (server != null) {
server.stop();
}
if (client != null) {
client.stop();
}
}
/**
* <p>命令处理函数,实现CommandListener接口</p>
* @param Command c 命令对象
* @param Displayable d 被放置到显示屏显示的对象
*/
public void commandAction(Command c, Displayable d) {
if (c == CMD_EXIT) {
try {
destroyApp(true);
} catch (MIDletStateChangeException e) {
e.printStackTrace();
}
notifyDestroyed();
} else if (c == CMD_START) {
String name = cg.getString(cg.getSelectedIndex());
if (name.equals(SERVER)) {
server = new Server(this);
server.start();
} else {
client = new Client(this);
client.start();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -