📄 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 { Command sendCommand = new Command("OK", Command.OK, 1); Command exitCommand = new Command("Exit", Command.EXIT, 2); Alert content; Display display; Thread thread; String[] connections; boolean done; String smsPort; int length1; MessageConnection smsconn; Message msg; TextBox messageBox; Displayable backScreen; int count; String senderAddress; /** Alert that is displayed when replying */ // Alert sendingMessageAlert; /** Prompts for and sends the text reply */ Displayable resumeScreen; public SMSReceive() { count=0; smsPort = getAppProperty("SMS-Port"); display = Display.getDisplay(this); this.backScreen = backScreen; content = new Alert("SMS Receive"); content.setTimeout(Alert.FOREVER); content.addCommand(exitCommand); content.setCommandListener(this); content.setString("Receiving..."); messageBox = new TextBox("Please input your auto-reply message", null, 65535, TextField.ANY); messageBox.addCommand(exitCommand); messageBox.addCommand(sendCommand); messageBox.setCommandListener(this); display.setCurrent(messageBox); } public void commandAction(Command c, Displayable s) { try { if (c == exitCommand || c == Alert.DISMISS_COMMAND) { destroyApp(false); notifyDestroyed(); }else if (c == sendCommand) { startApp(); resumeScreen = content; } } catch (Exception ex) { ex.printStackTrace(); } } 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("You have got "+count+" Message"); } 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(); count++; String add=address + ":" + smsPort; content.setString("You have got "+ count+ " Message(s)"); display.setCurrent(content); if(address.length()>4) Send(add); startApp(); } }catch (Throwable e) { e.printStackTrace(); } } 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 Send(String address){ try{ TextMessage txtmessage = (TextMessage)smsconn.newMessage( MessageConnection.TEXT_MESSAGE); txtmessage.setAddress(address); txtmessage.setPayloadText(messageBox.getString()); smsconn.send(txtmessage); }catch (Exception t) { } /*if(smsconn!=null) { try{ smsconn.close(); }catch(IOException ioe){ ioe.printStackTrace(); } }*/ }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -