×

Discussion Board

Results 1 to 3 of 3
  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    2

    Inform Client using Class that implements MessageListener to receive SMS

    Hi,
    I have been developping a simple SMS Client receiver//sender (i just send SMS to myself and receive them) that uses a SMS Class which implements MessageListener. When the notifyIncomingMessage is called, I would like that the Client get informed that a message arrived.
    Do I have to fill some kind of vector in the SMSclass when a message arrives, and in the client just verify with a loop ,after xxx seconds, if there is something new in that Vector?

    My code looks like that

    CLIENT
    Code:
    package sms;
    
    import javax.microedition.lcdui.Command;
    import javax.microedition.lcdui.*;
    import javax.microedition.midlet.MIDlet;
    
    public class Client extends MIDlet implements CommandListener{
    	SMS sms;
    	private Form form; 
    	private Command exitCmd; 
    	private Display display; 
    	
    	public Client(){
    		form = new Form("SMS Receive");
    		exitCmd = new Command("Exit", Command.EXIT, 2);
    		display = Display.getDisplay(this);
    		form.addCommand(exitCmd);
    		form.setCommandListener(this);
    		display.setCurrent(form);
    	}
    	
    	public void startApp() {
    		sms=new SMS();
    		sms.connect();
    		try{
    			Thread.sleep(3000);
    		}catch(InterruptedException e){
    			
    		}
    		sms.sendMessage("Test Message");
    
    		// HOW TO PRINT INCOMING MESSAGES?
    	}	
    	
    	public void pauseApp() {
    		sms.done = true;
    	}
    	
    
    	public void destroyApp(boolean unconditional) {
    		sms.done = true;
    		sms.closeConnection();
    	}
    
    
    	public void commandAction(Command c, Displayable s) {
    		if (c == exitCmd) {
    			destroyApp(false);
    			notifyDestroyed();
    		}
    	}
    
    }

    SMS MESSAGING

    Code:
    package sms;
    import java.io.IOException;
    import javax.microedition.io.*;
    import javax.wireless.messaging.*;
    
    public class SMS implements MessageListener {
    	MessageConnection msgConnIn, msgConnOut;
    	boolean done;
    	Reader reader;
    	
    public void connect(){
    	try {
    		msgConnIn = (MessageConnection)Connector.open("sms://:6222");
    		msgConnOut= (MessageConnection)Connector.open("sms://+5550000:6222");
    		msgConnIn.setMessageListener(this);
    		done = false;
    		reader = new Reader();
    	new Thread(reader).start();
    	} catch (IOException e) {
    		// Handle startup errors
    	}
    }
    
    
    public int sendMessage(String msgToSend){
    	try{
    		TextMessage tmsg =(TextMessage)msgConnOut.newMessage(MessageConnection.TEXT_MESSAGE);
            tmsg.setAddress("sms://+5550000:1000");
            tmsg.setPayloadText(msgToSend);
            msgConnOut.send(tmsg);
            return 1;
    	}catch(IOException ioe){
    		return -1;
    	}
    }	
    	
    
    public void notifyIncomingMessage(MessageConnection conn) {
    	if (conn == msgConnIn) {
    	reader.handleMessage();
    	}
    }
    
    
    public void closeConnection(){
    	try {
    		msgConnIn.setMessageListener(null);
    		msgConnIn.close();
    		msgConnOut.close();
    	}catch (IOException e) {
    	}
    }
    
    
    // Isolate blocking I/O on a separate thread, so callback
    // can return immediately.
    class Reader implements Runnable {
    	private int pendingMessages = 0;
    
    // The run method performs the actual message reading.
    public void run() {
    	while (!done) {
    		synchronized(this) {
    			if (pendingMessages == 0) {
    				try {
    					wait();
    				} catch (Exception e) {
    				// Handle interruption
    				}
    			}
    		pendingMessages--;
    	}	
    		try {
    			Message mess = msgConnIn.receive();
                                           // DO SOMETHING BUT WHAT?
    		} catch (IOException ioe) {
    			// Handle reading errors
    		}
    	}
    }
    
    	
    public synchronized void handleMessage() {
    	pendingMessages++;
    	notify();
    }
    
    }
    }
    How can I print on the screen incoming messages for the Client? How do I synchronize Client and SMSMessaging?

    Thanks for you answers!

    Damien
    Last edited by damHB; 2006-11-08 at 11:20.

  2. #2
    Registered User
    Join Date
    Nov 2006
    Posts
    2

    Re: Inform Client using Class that implements MessageListener to receive SMS

    solved.

    Thanks

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    2

    Re: Inform Client using Class that implements MessageListener to receive SMS

    Can you pls share how was it solved?

Similar Threads

  1. Replies: 4
    Last Post: 2010-06-11, 14:07
  2. Replies: 3
    Last Post: 2006-10-18, 06:48
  3. Receive SMS in J2ME application from GRPS modem (AT-commands)
    By gonzi in forum Mobile Java Networking & Messaging & Security
    Replies: 0
    Last Post: 2006-10-10, 00:16
  4. Read class Zero SMS (via RS232)
    By Vir@s in forum General Messaging
    Replies: 1
    Last Post: 2005-02-02, 08:15
  5. SMS general question: CSmsSettings class
    By abhishekmishra in forum Symbian C++
    Replies: 1
    Last Post: 2002-06-20, 09:13

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •