📄 wmaexample.java
字号:
package ch09.section06;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import javax.wireless.messaging.*;
import java.io.*;
import java.lang.*;
import java.util.*;
public class WMAExample
extends MIDlet
implements CommandListener, MessageListener {
private Command exitCommand;
private Command getMsgCommand;
private Display display;
Form displayForm;
String msgReceived;
MessageConnection serverConn;
public WMAExample() {
display = Display.getDisplay(this);
exitCommand = new Command("Exit", Command.SCREEN, 1);
getMsgCommand = new Command("Get", Command.SCREEN, 1);
}
public void startApp() {
displayForm = new Form("Get Message");
displayForm.addCommand(exitCommand);
displayForm.addCommand(getMsgCommand);
displayForm.setCommandListener(this);
displayForm.setItemStateListener(this);
display.setCurrent(displayForm);
try {
serverConn = (MessageConnection) Connector.open
("sms://:5000");
serverConn.setMessageListener(this);
}
catch (IOException ioExc) {
System.out.println("Server connection could not be obtained");
ioExc.printStackTrace();
}
}
public void notifyIncomingMessage(MessageConnection conn) {
Message msg = null;
try {
msg = conn.receive();
}
catch (Exception e) {
System.out.println("processMessage.receive " + e);
}
if (msg instanceof TextMessage) {
TextMessage tmsg = (TextMessage) msg;
msgReceived = tmsg.getPayloadText();
}
else {
if (msg instanceof BinaryMessage) {
BinaryMessage bmsg = (BinaryMessage) msg;
byte[] data = bmsg.getPayloadData();
msgReceived = data.toString();
}
}
}
public void destroyApp(boolean unconditional) {
try {
if (serverConn != null) {
serverConn.setMessageListener(null);
serverConn.close();
}
}
catch (IOException e) {
e.printStacktrace();
}
}
public void commandAction(Command c, Displayable s) {
if (c == exitCommand) {
destroyApp(false);
notifyDestroyed();
}
if (c == getMsgCommand) {
try {
displayForm.append(msgReceived);
display.setCurrent(displayForm);
}
catch (Exception exc) {
exc.printStackTrace();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -