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

📄 smsrecv.java

📁 Socket程序
💻 JAVA
字号:
//SmsRecv.java		2004/04/02	tana

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import javax.wireless.messaging.*;

import java.io.*;

public class SmsRecv implements CommandListener, MessageListener
{
	Command backCommand = new Command( "Back", Command.BACK, 1 );
		
	//parameters used in sms 
    	Alert content;
    	MessageConnection smsconn;
    	MessageConnection currentconn;
    	Message msg;
    	SmsThread smsth;
    	String smsPort;
    	String senderAddr;
    	String[] connections;
    	Displayable backScreen;
    	Display display;
    	boolean done;
	
	public SmsRecv( Displayable backScreen, Display display )
	{
		this.backScreen = backScreen;
		this.display = display;
		smsPort = "16105" ;
		content = new Alert( "SMS Receive" );
		content.setTimeout( Alert.FOREVER );
		content.addCommand( backCommand );
		content.setCommandListener( this );
		
	}
	
	public void start()
	{
		String smsConnection = "sms://:" + smsPort;
      		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( "Waiting for SMS on port " + smsPort + "..." );
    		}
    		
    		currentconn = smsconn;//给出当前连接
    		done = false;
    		smsth = new SmsThread();
    		smsth.start();
    		
    		display.setCurrent( content );
	}
	
	public void notifyIncomingMessage( MessageConnection conn )
	{
		currentconn = conn;
    		if( smsth==null )
    		{
    			done = false;
    			smsth = new SmsThread();
    			smsth.start();
    		}
	}
	
	
	public void commandAction( Command c, Displayable s )
	{
		if ( c==backCommand )
        	{
        		display.setCurrent( backScreen );
        		smsth = null;
        		done = true;
               		if ( smsconn!=null )
        		{
            	    		try{
               				smsconn.close();
            	    		}catch ( IOException e )
            	    		{}
        		}
        	}
	}
	
	class SmsThread extends Thread
	{
   		public void run()
    		{
    		    try{    	
    		    	while( !done )
    		    	{
    		    		msg = currentconn.receive();
    	    			if ( msg!=null )
    	    			{	
	    	    			content.setString( ((TextMessage)msg).getPayloadText() );  		    			
    	    			}
    	    		}
    	    		currentconn.close();
    	    	    	}catch( IOException ioe )
    	    	    {}
    		}
    	}   
}

⌨️ 快捷键说明

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