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
How can I print on the screen incoming messages for the Client? How do I synchronize Client and SMSMessaging?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(); } } }
Thanks for you answers!
Damien

Reply With Quote

