⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 smsrepeatreceive.java

📁 不断发短信的j2me 程序
💻 JAVA
字号:
//SMSRepetaReceive.java		2004/03/29	ta na//该程序能接收短消息,并将其内容显示出来(只限文本消息,而且不能太长)import javax.microedition.midlet.*;import javax.microedition.io.*;import javax.microedition.lcdui.*;import javax.wireless.messaging.*;import java.io.IOException;public class SMSRepeatReceive extends MIDlet implements CommandListener, Runnable, MessageListener {    Command exitCommand  = new Command("Exit", Command.EXIT, 1 );    Command okCommand = new Command("Ok", Command.OK, 1);    Command finishCommand = new Command( "Finish", Command.OK, 2 );        Display display;    Thread th;    TextBox contents;    String smsPort;    MessageConnection smsconn;    Message msg;    String senderAddress;    Form myForm;    recvThread recv;    boolean first = true;    Alert recvAlert;            public SMSRepeatReceive()     {        display = Display.getDisplay(this);        contents = new TextBox( "Input Port Number", null, 256, TextField.NUMERIC );         contents.addCommand( exitCommand );        contents.addCommand( okCommand );         contents.setCommandListener( this );	recvAlert = new Alert( "Recving Alert", null, null, AlertType.INFO );	recvAlert.setTimeout( Alert.FOREVER );	recvAlert.addCommand( exitCommand );	recvAlert.setCommandListener( this );		myForm = new Form ( "Received text message" );	myForm.addCommand( finishCommand );	myForm.setCommandListener( this );	    }       public void startApp()     {    	display.setCurrent( contents );    }         public void pauseApp()     {     }       public void destroyApp(boolean unconditional)     {        th = null;            }      public void commandAction(Command c, Displayable s)     {        try {            if (c == exitCommand )             {                destroyApp(false);                notifyDestroyed();            }            else             	if (c == okCommand)             	{            		th = new Thread(this);            		th.start();            	}			            	      	            	else            	    if( c==finishCommand )            	    {            	    	if (smsconn != null)            	    	{            			try {                			smsconn.close();            			} catch (IOException e) {                		// Ignore any errors on shutdown            			}        		}        		first = true;        		myForm.deleteAll();//清空纪录            	    	display.setCurrent( contents );            	    }             	        } catch (Exception ex)         {            ex.printStackTrace();        }    }    public void run()     {    	smsPort = contents.getString();    	String smsConnection = "sms://:" + smsPort;                try {             	smsconn = (MessageConnection) Connector.open(smsConnection);               	smsconn.setMessageListener(this);        } catch (IOException ioe) {	        ioe.printStackTrace();        }                String tmpstr = "Waiting for incoming message on " + smsPort;        recvAlert.setString( tmpstr );        display.setCurrent( recvAlert );     	recv = new recvThread();     	recv.start();    }                    public void notifyIncomingMessage(MessageConnection conn)     {    	recv = new recvThread();    	recv.start();    	      }     class recvThread extends Thread   {   	public void run()     	{        	            	try {	            	msg = smsconn.receive();            		if (msg != null)             		{            			if ( first )            			{            				senderAddress = msg.getAddress();	                		String info = "Message from " + senderAddress;	                		myForm.append( info );                			first = false;                		}	                	if (msg instanceof TextMessage) 	                	{	                    		String data = ((TextMessage)msg).getPayloadText();                    			myForm.append( data );                		}	                	}                	display.setCurrent(myForm);            	} catch (IOException e)             	{          		// e.printStackTrace();        	}        }    }                }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -