smsreceive.java

来自「J2ME核心类及MIDlet类 MIDP用户界面对象 图形处理及低级事件处」· Java 代码 · 共 109 行

JAVA
109
字号
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 {       Command exitCommand  = new Command("Exit", Command.EXIT, 2);    Alert content;    Display display;    Thread thread;    String[] connections;    boolean done;    String smsPort;    MessageConnection smsconn;    Message msg;    Displayable resumeScreen;    public SMSReceive() {        smsPort = getAppProperty("SMS-Port");        display = Display.getDisplay(this);        content = new Alert("SMS Receive");        content.setTimeout(Alert.FOREVER);        content.addCommand(exitCommand);        content.setCommandListener(this);        content.setString("Receiving...");                resumeScreen = content;    }    public void startApp() {	    String smsConnection = "sms://:" + smsPort;        if (smsconn == null) {            try {                smsconn = (MessageConnection) Connector.open(smsConnection);                smsconn.setMessageListener(this);            } catch (IOException ioe) {                ioe.printStackTrace();            }        }        connections = PushRegistry.listConnections(true);        if (connections == null || connections.length == 0) {            content.setString("等待短信 " + smsPort + "...");        }        done = false;        thread = new Thread(this);        thread.start();        display.setCurrent(resumeScreen);    }    public void notifyIncomingMessage(MessageConnection conn) {        if (thread == null) {            done = false;            thread = new Thread(this);            thread.start();        }    }    public void run() {		            try {            msg = smsconn.receive();            if (msg != null && msg instanceof TextMessage) {                String address = msg.getAddress();                 content.setTitle("From: " + address);                content.setString(((TextMessage)msg).getPayloadText());                display.setCurrent(content);            }        } catch (IOException e) {        }    }    public void pauseApp() {        done = true;        thread = null;        resumeScreen = display.getCurrent();    }    public void destroyApp(boolean unconditional) {        done = true;         thread = null;        if (smsconn != null) {            try {                smsconn.close();            } catch (IOException e) {            }        }    }	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 + =
减小字号Ctrl + -
显示快捷键?