I have an application to send and recieve an SMS but my application is not catching the incoming message.
can anyone tell what's wrong with the following code:
Expand|Select|Wrap|Line Numbers
- import javax.microedition.midlet.*;
- import javax.microedition.lcdui.*;
- import javax.microedition.io.*;
- import javax.wireless.messaging.*;
- import java.io.*;
- import java.util.*;
- public class MessageReceive extends MIDlet implements CommandListener, MessageListener
- {
- private Command exitCommand;
- private Command getMsgCommand;
- private Display display;
- Form displayForm;
- public static String msgReceived ;
- Thread Recivethread;
- MessageConnection serverConn;
- public MessageReceive () {
- display = Display.getDisplay(this);
- exitCommand =
- new Command("Exit", Command.SCREEN, 1);
- getMsgCommand =
- new Command("Get", Command.SCREEN, 1);
- }
- public void startApp() {
- displayForm = new Form("Get Message");
- displayForm.addCommand(exitCommand);
- displayForm.addCommand(getMsgCommand);
- displayForm.setCommandListener(this);
- display.setCurrent(displayForm);
- try
- {
- Alert rrrecAlert = new Alert("","Mesage connection open",null, AlertType.INFO);
- rrrecAlert.setTimeout(Alert.FOREVER);
- display.setCurrent(rrrecAlert, displayForm);
- serverConn = (MessageConnection)Connector.open("sms://:5000",Connector.READ);
- serverConn.setMessageListener(this);
- Alert ecAlert = new Alert("","Mesage Listener.....",null, AlertType.INFO);
- ecAlert.setTimeout(Alert.FOREVER);
- display.setCurrent(ecAlert, displayForm);
- Recivethread = new Thread(new CatchRecMsg());
- Recivethread.start();
- }
- catch (IOException ioExc)
- {
- Alert ecAlert = new Alert("","Server connection could not be obtained",null, AlertType.INFO);
- ecAlert.setTimeout(Alert.FOREVER);
- display.setCurrent(ecAlert, displayForm);
- }
- }
- public synchronized void notifyIncomingMessage(MessageConnection conn) {
- Alert ecAlert = new Alert("","Mesage Incoming Listener.....",null, AlertType.INFO);
- ecAlert.setTimeout(Alert.FOREVER);
- display.setCurrent(ecAlert, displayForm);
- if (conn == serverConn) {
- Recivethread = new Thread(new CatchRecMsg());
- Recivethread.start();
- }
- else{
- Alert eAlert = new Alert("","Mesage conn not equals.....",null, AlertType.INFO);
- eAlert.setTimeout(Alert.FOREVER);
- display.setCurrent(eAlert, displayForm);
- }
- }
- public class CatchRecMsg implements Runnable
- {
- public void run() {
- try{
- Alert recAlert = new Alert("","This is getting",null, AlertType.INFO);
- recAlert.setTimeout(Alert.FOREVER);
- display.setCurrent(recAlert, displayForm);
- try {
- System.out.println("In the catchReceive ");
- Message msg = serverConn.receive();
- System.out.println("Out the the catchReceive ");
- if (msg instanceof TextMessage) {
- TextMessage tmsg = (TextMessage)msg;
- msgReceived = tmsg.getPayloadText();
- recAlert = new Alert("","We got the masg Text : "+msgReceived,null, AlertType.INFO);
- recAlert.setTimeout(Alert.FOREVER);
- display.setCurrent(recAlert, displayForm);
- displayForm.append(msgReceived);
- display.setCurrent(displayForm);
- }
- else
- if (msg instanceof BinaryMessage) {
- BinaryMessage bmsg = (BinaryMessage)msg;
- byte[] data = bmsg.getPayloadData();
- msgReceived = data.toString();
- displayForm.append(msgReceived);
- display.setCurrent(displayForm);
- recAlert = new Alert("","We got the masg Binary : " +msgReceived,null, AlertType.INFO);
- recAlert.setTimeout(Alert.FOREVER);
- display.setCurrent(recAlert, displayForm);
- }
- else
- {
- recAlert = new Alert("","Msg getting is nothing"+msg,null, AlertType.INFO);
- recAlert.setTimeout(Alert.FOREVER);
- display.setCurrent(recAlert, displayForm);
- }
- }
- catch (Exception e) {
- recAlert = new Alert("","Error occurs in receive()"+e,null, AlertType.INFO);
- recAlert.setTimeout(Alert.FOREVER);
- display.setCurrent(recAlert, displayForm);
- }
- } catch (Exception e) {
- Alert recAlert = new Alert("","I got error here",null, AlertType.INFO);
- recAlert.setTimeout(Alert.FOREVER);
- display.setCurrent(recAlert, displayForm);
- }
- }
- }
- public void pauseApp()
- { }
- public void destroyApp(boolean unconditional) {
- }
- public void commandAction(
- Command c, Displayable s) {
- if (c == exitCommand) {
- destroyApp(false);
- notifyDestroyed();
- }
- if (c == getMsgCommand) {
- try
- {
- displayForm.append(msgReceived);
- display.setCurrent(displayForm);
- Alert ecAlert = new Alert("","nothing there .....",null, AlertType.INFO);
- ecAlert.setTimeout(Alert.FOREVER);
- display.setCurrent(ecAlert, displayForm);
- }
- catch (Exception exc)
- {
- exc.printStackTrace();
- }
- }
- }
- }
regards
LTCCTL