📄 smsreceive.java
字号:
import javax.microedition.midlet.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
import javax.wireless.messaging.*;
import java.io.IOException;
public class SMSReceive extends MIDlet
implements CommandListener, Runnable, MessageListener {
Display display;
Command exitCommand = new Command("Exit", Command.EXIT, 2);
//用于显示接收到的信息
Alert content;
//消息发送者的地址
String senderAddress;
//程序被激活时要显示的屏幕
Displayable resumeScreen;
String smsPort;
MessageConnection smsconn;
Message msg;
public SMSReceive() {
smsPort ="50000";
content = new Alert("SMS Receive");
content.setTimeout(Alert.FOREVER);
content.addCommand(exitCommand);
content.setCommandListener(this);
resumeScreen = content;
}
public void startApp() {
display = Display.getDisplay(this);
//打开SMS消息连结
String smsConnection = "sms://:" + smsPort;
if (smsconn == null) {
try {
smsconn = (MessageConnection) Connector.open(smsConnection);
smsconn.setMessageListener(this);
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
content.setString("Waiting for SMS on port " + smsPort + "...");
display.setCurrent(resumeScreen);
}
public void notifyIncomingMessage(MessageConnection conn) {
Thread t = new Thread(this);
t.start();
}
public void run() {
try {
String msginfo=null;
//接收消息
msg = smsconn.receive();
if (msg != null) {
senderAddress = msg.getAddress();
content.setTitle("From: " + senderAddress);
//处理文本消息
if (msg instanceof TextMessage) {
msginfo="Message :"+((TextMessage)msg).getPayloadText()
+"\n"+"From :"+((TextMessage)msg).getAddress()+"\n";
content.setString(msginfo);
}
//处理二进制消息
else {
StringBuffer buf = new StringBuffer();
byte[] data = ((BinaryMessage)msg).getPayloadData();
for (int i = 0; i < data.length; i++) {
buf.append(Integer.toHexString((int) data[i]));
buf.append(' ');
}
content.setString(buf.toString());
}
//显示接收到的消息
display.setCurrent(content);
}
} catch (IOException e) {
e.printStackTrace();
}
}
public void pauseApp() {
resumeScreen = display.getCurrent();
}
public void destroyApp(boolean unconditional) {
if (smsconn != null) {
try {
smsconn.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void commandAction(Command c, Displayable s) {
try {
if (c == exitCommand || c == Alert.DISMISS_COMMAND) {
destroyApp(false);
notifyDestroyed();
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -