Saturday, August 27, 2011

J2ME: How To Receive SMS in J2ME


i make sms which can receive by J2ME app, now, this is my sample code. it’s call J2ME push registry, so when i send sms, my J2ME app will launch automaticly
public class sampleSmsPushRegistry extends MIDlet
   implements CommandListener, Runnable, MessageListener {

   public void startApp() {
      smsPort = getAppProperty("SMS-Port");
      String smsConnection = "sms://:" + smsPort;
      if (smsconn == null) {
         try {
            smsconn = (MessageConnection)
               Connector.open(smsConnection);

            smsconn.setMessageListener(this);
         } catch (IOException ioe) {
            ioe.printStackTrace();
         }
      }
      display.setCurrent(resumeScreen);
   }
   public void notifyIncomingMessage(MessageConnection conn) {
      if (thread == null) {
         thread = new Thread(this);
         thread.start();
      }
   }
   public void run() {
      try {
         msg = smsconn.receive();
         if (msg != null) {
            if (msg instanceof TextMessage) {

               content.setString(((TextMessage)msg).getPayloadText());
            }
            display.setCurrent(content);
         }
      } catch (IOException e) {
         e.printStackTrace();
      }
   }

}


No comments: