📄 smsrepeatsend.java
字号:
//SMSRepeatSend.java 2004/03/25//该程序可以循环发送文本信息import javax.microedition.midlet.*;import javax.microedition.io.*;import javax.microedition.lcdui.*;import javax.wireless.messaging.*;import java.io.IOException;public class SMSRepeatSend extends MIDlet implements CommandListener, Runnable { Command exitCommand = new Command("Exit", Command.EXIT, 1); Command okCommand = new Command("OK", Command.OK, 1); Command backCommand = new Command("Back", Command.BACK, 2); Command sendCommand = new Command( "Send", Command.OK, 2 ); Display display; String smsPort; TextBox destAddrBox; TextBox destPortBox; Alert errorMsgAlert; Alert infoAlert; Displayable resumeScreen = null; String addr; int REPEATNUM = 50; int count = 0;//计数器 String data = "Hello? sending message test"; Thread th; Thread tempth; Form sendForm;//显示发送的内容 SendThread sender; public SMSRepeatSend() { display = Display.getDisplay(this); destAddrBox = new TextBox("Destination Address?", null, 256, TextField.PHONENUMBER); destAddrBox.addCommand(exitCommand); destAddrBox.addCommand(okCommand); destAddrBox.setCommandListener(this); destPortBox = new TextBox( "Input Port(16000-17000)", null, 256, TextField.PHONENUMBER ); destPortBox.addCommand( backCommand ); destPortBox.addCommand( sendCommand ); destPortBox.setCommandListener( this ); sendForm = new Form( "Sending Window" ); sendForm.addCommand( backCommand ); sendForm.setCommandListener( this ); infoAlert = new Alert( "SMS Send", "Message sending finished", null, AlertType.INFO ); infoAlert.setTimeout( 2000 ); errorMsgAlert = new Alert("SMS Error Message", null, null, AlertType.ERROR); errorMsgAlert.setTimeout(5000); resumeScreen = destAddrBox; } public void startApp() { display.setCurrent(resumeScreen); } public void pauseApp() { resumeScreen = display.getCurrent(); } public void destroyApp(boolean unconditional) { th = null; } public void commandAction(Command c, Displayable s) { try { if (c == exitCommand || c == Alert.DISMISS_COMMAND) { destroyApp(false); notifyDestroyed(); } else if (c == okCommand) display.setCurrent( destPortBox ); else if( c==backCommand ) { count = 0;//重新计数 sendForm.deleteAll(); display.setCurrent( destAddrBox ); } else if ( c==sendCommand ) { addr = destAddrBox.getString(); if ( !SMSRepeatSend.isValidPhoneNumber( addr ) ) { errorMsgAlert.setString( "Invalid phone number" ); display.setCurrent( errorMsgAlert, destAddrBox ); } smsPort = destPortBox.getString(); String tmpstr = "Send to +" + addr + "://:" + smsPort; sendForm.append( tmpstr ); display.setCurrent( sendForm ); //循环发送短信 Thread th = new Thread(this); th.start(); } } catch (Exception ex) { ex.printStackTrace(); } } private static boolean isValidPhoneNumber(String number) { char[] chars = number.toCharArray(); if (chars.length == 0) { return false; } int startPos = 0; // initial '+' is OK if (chars[0]=='+') { startPos = 1; } for (int i = startPos; i < chars.length; ++i) { if (!Character.isDigit(chars[i])) { return false; } } return true; } public void run() { sender = new SendThread(); sender.start();//先启动一个线程,去发送短信,然后等它结束后再去启动另一个线程 for ( int i=0; i<REPEATNUM-1; i++ ) { try{ sender.join();//等待当前发送短信的线程结束 }catch ( InterruptedException ie ) { ie.printStackTrace(); } sender = new SendThread(); sender.start(); } display.setCurrent( infoAlert ); } class SendThread extends Thread { public void run() { count++; String destAddr = "sms://" + addr + ":" + smsPort; MessageConnection smsconn = null; try { smsconn = (MessageConnection)Connector.open( destAddr ); TextMessage textmessage = (TextMessage)smsconn.newMessage( MessageConnection.TEXT_MESSAGE ); textmessage.setAddress( destAddr ); String content = count + "---" + data ; textmessage.setPayloadText( content );//文本内容 sendForm.append( content ); smsconn.send( textmessage ); } catch ( Throwable t ) { System.out.println( "Send caught:"); t.printStackTrace(); } if ( smsconn!=null ) { // System.out.println( count + "-----closed" ); try { smsconn.close(); }catch ( IOException ioe ) { System.out.println( "Closing connection caught:" ); ioe.printStackTrace(); } } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -