Connecting Tech Pros Worldwide Help | Site Map

Problem in capturing incoming message

Member
 
Join Date: Feb 2008
Location: Bangalore
Posts: 49
#1: Dec 24 '08
hi all,

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
  1.  
  2. import javax.microedition.midlet.*;
  3. import javax.microedition.lcdui.*;
  4. import javax.microedition.io.*;
  5. import javax.wireless.messaging.*;
  6. import java.io.*;
  7.  
  8. import java.util.*;
  9.  
  10.  
  11. public class MessageReceive extends MIDlet implements CommandListener, MessageListener 
  12. {
  13.  
  14.  
  15. private Command exitCommand; 
  16.  
  17. private Command getMsgCommand;
  18.  
  19.  
  20. private Display display;
  21.  
  22. Form displayForm;
  23. public static String msgReceived ;
  24. Thread Recivethread;
  25.  
  26. MessageConnection serverConn;
  27.  
  28.  
  29.  
  30.  
  31.  
  32. public MessageReceive () {
  33. display = Display.getDisplay(this);
  34. exitCommand = 
  35. new Command("Exit", Command.SCREEN, 1);
  36. getMsgCommand = 
  37. new Command("Get", Command.SCREEN, 1);
  38.  
  39.  
  40. }
  41.  
  42.  
  43. public void startApp() {
  44. displayForm = new Form("Get Message");
  45.  
  46. displayForm.addCommand(exitCommand);
  47. displayForm.addCommand(getMsgCommand);
  48.  
  49. displayForm.setCommandListener(this);
  50.  
  51. display.setCurrent(displayForm);
  52.  
  53. try
  54. {
  55. Alert rrrecAlert = new Alert("","Mesage connection open",null, AlertType.INFO);
  56. rrrecAlert.setTimeout(Alert.FOREVER);
  57. display.setCurrent(rrrecAlert, displayForm);
  58. serverConn = (MessageConnection)Connector.open("sms://:5000",Connector.READ);
  59.  
  60. serverConn.setMessageListener(this);
  61. Alert ecAlert = new Alert("","Mesage Listener.....",null, AlertType.INFO);
  62. ecAlert.setTimeout(Alert.FOREVER);
  63. display.setCurrent(ecAlert, displayForm);
  64. Recivethread = new Thread(new CatchRecMsg());
  65. Recivethread.start();
  66.  
  67. }
  68. catch (IOException ioExc)
  69. {
  70. Alert ecAlert = new Alert("","Server connection could not be obtained",null, AlertType.INFO);
  71. ecAlert.setTimeout(Alert.FOREVER);
  72. display.setCurrent(ecAlert, displayForm);
  73.  
  74.  
  75. }
  76.  
  77.  
  78. public synchronized void notifyIncomingMessage(MessageConnection conn) {
  79.  
  80. Alert ecAlert = new Alert("","Mesage Incoming Listener.....",null, AlertType.INFO);
  81. ecAlert.setTimeout(Alert.FOREVER);
  82. display.setCurrent(ecAlert, displayForm);
  83. if (conn == serverConn) {
  84.  
  85. Recivethread = new Thread(new CatchRecMsg());
  86. Recivethread.start();
  87. }
  88. else{
  89. Alert eAlert = new Alert("","Mesage conn not equals.....",null, AlertType.INFO);
  90. eAlert.setTimeout(Alert.FOREVER);
  91. display.setCurrent(eAlert, displayForm);
  92.  
  93. }
  94.  
  95. }
  96.  
  97. public class CatchRecMsg implements Runnable
  98. {
  99. public void run() {
  100. try{
  101.  
  102. Alert recAlert = new Alert("","This is getting",null, AlertType.INFO);
  103. recAlert.setTimeout(Alert.FOREVER);
  104. display.setCurrent(recAlert, displayForm);
  105.  
  106. try {
  107. System.out.println("In the catchReceive ");
  108.  
  109. Message msg = serverConn.receive();
  110. System.out.println("Out the the catchReceive ");
  111. if (msg instanceof TextMessage) {
  112. TextMessage tmsg = (TextMessage)msg;
  113. msgReceived = tmsg.getPayloadText();
  114. recAlert = new Alert("","We got the masg Text : "+msgReceived,null, AlertType.INFO);
  115. recAlert.setTimeout(Alert.FOREVER);
  116. display.setCurrent(recAlert, displayForm);
  117. displayForm.append(msgReceived);
  118. display.setCurrent(displayForm);
  119.  
  120. }
  121. else 
  122.  
  123. if (msg instanceof BinaryMessage) {
  124. BinaryMessage bmsg = (BinaryMessage)msg;
  125. byte[] data = bmsg.getPayloadData();
  126.  
  127. msgReceived = data.toString();
  128. displayForm.append(msgReceived);
  129. display.setCurrent(displayForm);
  130.  
  131. recAlert = new Alert("","We got the masg Binary : " +msgReceived,null, AlertType.INFO);
  132. recAlert.setTimeout(Alert.FOREVER);
  133. display.setCurrent(recAlert, displayForm);
  134. }
  135.  
  136. else
  137. {
  138. recAlert = new Alert("","Msg getting is nothing"+msg,null, AlertType.INFO);
  139. recAlert.setTimeout(Alert.FOREVER);
  140. display.setCurrent(recAlert, displayForm);
  141. }
  142. }
  143. catch (Exception e) {
  144. recAlert = new Alert("","Error occurs in receive()"+e,null, AlertType.INFO);
  145. recAlert.setTimeout(Alert.FOREVER);
  146. display.setCurrent(recAlert, displayForm);
  147. }
  148.  
  149. } catch (Exception e) {
  150. Alert recAlert = new Alert("","I got error here",null, AlertType.INFO);
  151. recAlert.setTimeout(Alert.FOREVER);
  152. display.setCurrent(recAlert, displayForm);
  153. }
  154. }
  155.  
  156.  
  157.  
  158.  
  159. public void pauseApp()
  160. { }
  161.  
  162.  
  163.  
  164. public void destroyApp(boolean unconditional) { 
  165.  
  166.  
  167.  
  168. }
  169.  
  170.  
  171. public void commandAction(
  172. Command c, Displayable s) {
  173. if (c == exitCommand) {
  174. destroyApp(false);
  175. notifyDestroyed();
  176. }
  177.  
  178. if (c == getMsgCommand) {
  179. try
  180. {
  181.  
  182. displayForm.append(msgReceived);
  183.  
  184. display.setCurrent(displayForm);
  185. Alert ecAlert = new Alert("","nothing there .....",null, AlertType.INFO);
  186. ecAlert.setTimeout(Alert.FOREVER);
  187. display.setCurrent(ecAlert, displayForm);
  188.  
  189. }
  190. catch (Exception exc)
  191. {
  192. exc.printStackTrace();
  193. }
  194.  
  195.  
  196. }
  197. }
  198.  
  199.  
  200.  
  201. }
  202.  


regards
LTCCTL
Reply


Similar Mobile Development bytes